I have a dropdown that I am trying to populate with the results from ActiveRecord. I guess I don't understand how to access the values that I'm retrieving because the code below is populating my dropdown with values like
#<Product:0x007f1f488565b0>
which obviously isn't what I want.
<%= f.select :accessory, options_for_select(Product.select(:item_number, :id).where(:accessory=> 't') {|c| [ c.item_number, c.id ] }), {include_blank: true}, { :class => 'form-control'} %>
How can I populate the drop down with the item number and id? There is no relationship involved in this query. I just want a list of products that have are marked with a "t" for accessory.
You are so close :-)
The parameter for options_for_select should be an object with an enumerable of objects which responds to :first and :last
You are passing
Product.select(:item_number, :id).where(:accessory=> 't')
as parameter. But my guess is that you want to pass
Product.select(:item_number, :id).where(:accessory=> 't')
.map{|c| [ c.item_number, c.id ] })
You simply forgot to put the 'map'.
Ruby allows you to pass a block to any method, that's why you don't get any warnings or errors, but the block will simply be ignored.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments