Populate Dropdown with Values from ActiveRecord query

mack

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.

Mark Meeus

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

rails 4 populate dropdown values from database

From Dev

PHP foreach loop to populate dropdown list with values from an array of arrays

From Dev

populate dropdown from class

From Dev

Populate/insert values in query

From Dev

extract attributes values from an activerecord query object in rails 2.3

From Dev

Populate column values from MS Access database into VB .Net Combo-Box dropdown values?

From Dev

Populate Dropdown using database query results

From Dev

Exclude Rails ActiveRecord from query

From Dev

Rails, use custom SQL query to populate ActiveRecord model

From Dev

angular populate dropdown from object laravel

From Dev

populate select dropdown with keys from JSON array

From Dev

How To Populate a DropDown List From a DataSet?

From Dev

Populate dropdown with data from database using jQuery

From Dev

Populate dropdown from entity framework db context

From Dev

Jquery populate textarea from dropdown list

From Dev

Populate Dropdown from XML with"text" & "value"

From Dev

Jquery populate textarea from dropdown list

From Dev

Populate dropdown menu from an XML file jquery

From Dev

Populate dropdown from entity framework db context

From Dev

Populate dropdown with information from a dataSource in Android

From Dev

Jquery 3.3.1 populate Dropdown from JSON object

From Dev

Populate database with values from hash

From Dev

Populate html dropdown list dynamically from other dropdown

From Dev

Populate a dropdown on selecting an option from another dropdown Laravel

From Dev

How to query multi values of enum field in ActiveRecord?

From Dev

dynamic query for different values rails 4 activerecord

From Dev

Select2 - Ajax Data - Populate The Dropdown based on query

From Dev

How can I populate dropdown values dynamically based on prior selection?

From Dev

Populate a second dropdown accoriding to the values returned by Jquery Ajax

Related Related

  1. 1

    rails 4 populate dropdown values from database

  2. 2

    PHP foreach loop to populate dropdown list with values from an array of arrays

  3. 3

    populate dropdown from class

  4. 4

    Populate/insert values in query

  5. 5

    extract attributes values from an activerecord query object in rails 2.3

  6. 6

    Populate column values from MS Access database into VB .Net Combo-Box dropdown values?

  7. 7

    Populate Dropdown using database query results

  8. 8

    Exclude Rails ActiveRecord from query

  9. 9

    Rails, use custom SQL query to populate ActiveRecord model

  10. 10

    angular populate dropdown from object laravel

  11. 11

    populate select dropdown with keys from JSON array

  12. 12

    How To Populate a DropDown List From a DataSet?

  13. 13

    Populate dropdown with data from database using jQuery

  14. 14

    Populate dropdown from entity framework db context

  15. 15

    Jquery populate textarea from dropdown list

  16. 16

    Populate Dropdown from XML with"text" & "value"

  17. 17

    Jquery populate textarea from dropdown list

  18. 18

    Populate dropdown menu from an XML file jquery

  19. 19

    Populate dropdown from entity framework db context

  20. 20

    Populate dropdown with information from a dataSource in Android

  21. 21

    Jquery 3.3.1 populate Dropdown from JSON object

  22. 22

    Populate database with values from hash

  23. 23

    Populate html dropdown list dynamically from other dropdown

  24. 24

    Populate a dropdown on selecting an option from another dropdown Laravel

  25. 25

    How to query multi values of enum field in ActiveRecord?

  26. 26

    dynamic query for different values rails 4 activerecord

  27. 27

    Select2 - Ajax Data - Populate The Dropdown based on query

  28. 28

    How can I populate dropdown values dynamically based on prior selection?

  29. 29

    Populate a second dropdown accoriding to the values returned by Jquery Ajax

HotTag

Archive