Forum: Ruby on Rails select list generated from table

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-01-25 21:26
(Received via mailing list)
Have 2 tables - clients & case_managers

class Client < ActiveRecord::Base
  has_one :case_manager

app/views/clients/_form.html contains line...

<p><% collection_select("client", "case_manager_id", \
case_manager.find_all, "name", "id") %></p>

and this line generates error...
undefined local variable or method `case_manager' for

What should the collection_select statement look like for it to populate
a pop-up select list of the names column from case_managers table?

for reference;

clients table has defined...
id integer primary key not null
case_manager_id not null
foreign key (case_manager_id) references case_managers(id)

case_managers table has defined
id integer primary key not null
name character varying 53 not null
no foreign keys

case_managers_clients table has defined
client_id integer not null
case_manager_id integer not null
no primary key
Fcd3b65b9d8d37d7cd91f3159b851b11?d=identicon&s=25 Miguel Enrique Cobá Martínez (Guest)
on 2006-01-26 18:58
(Received via mailing list)
Craig White wrote:

<p><% collection_select("client", "case_manager_id", \
CaseManager.find(:all), "id", "name") %></p>

supposing CaseManager is the model for your table
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-26 21:47
(Received via mailing list)
<% @case_managers = CaseManager.find(:all).collect {|c| [,]}
<%= select 'client', 'case_manager_id', @case_managers %>

Personally, I would put the first of the two statements in the
controller, because I don't like seeing find() in the view.

If you have a large number of Case Managers, you could write a
more-optimized way of building that list, and make it a class method
in CaseManager.
class CaseManager < ActiveRecord::Base
  def self.case_manager_options
    managers = connection.select_all("select name, id from
    managers.collect {|m| m.values}

Then, in your view, you can just do:
<%= select 'client', 'case_manager_id', CaseManager.case_manager_options

Warning: People will now complain that this is premature optimization.
 Only do it if it makes this easier/faster for you.  Myself, I like
having this sort of thing taken care of by the model.

P.S. The difference between select and collection_select is that
select takes care of selecting the current value for you.  Saves you a

Good luck,
This topic is locked and can not be replied to.