Search code examples
ruby-on-railslookup-tables

How do I get my look up table values in a select box?


Reading along with the agile book, it says this in a footnote:

... You simply pass the select helper the result of doing a find(:all) on your lookup table.

Okay ...

<%= f.select :source, Source.find(:all) %>

My source controller (and therefore table) looks like this:

create_table :sources do |t|
  t.string :source

  t.timestamps
end

But my select box is a mess, I get this type of data in all the values and displays:

#<Source:0x23a2bfc>

So I tried doing a Source.find(:all, :select => 'name,id') but that still gives me the whacky looking stuff.

What am I doing wrong?


Solution

  • Source.find(:all)
    

    will return an array of Source objects - this is not what you want. What you wants is an array of options. Something like:

    select(:source, "source_id", Source.all.collect {|p| [ p.source, p.id ] })