Select list


I’m trying to build a selection list which I have done in various ways
but this one is new to me.

I have a ‘facilities’ table which has all the outpatient facilities but
I need to add ‘Float’ and ‘Main Office’ which I don’t want to add to the
‘facilities’ table itself.

so I figure I can add these to an array created by…

def fac_list
@fac_list = Facility.find(:all) + [‘Float’, ‘Main Office’]

and I get an array of the facilities that I need but I can’t figure out
how to get this into a sorted list so I can pick this value in my view

<%= options = [[‘Select a Facility’, ‘’]] +
@fac_list.sort { |a,b| a.value <=> b.value }.collect {
|fac| [fac.value] }
select ‘personnel’, ‘facility’, options %>

but this only gets me an error of an undefined method ‘value’

How do I deal with this?



Hi Craig,

It looks like your variable @fac_list is going to end up looking

[ facility1,
'Main Office ]

So that when you say

fac_list.sort { |a,b| a.value <=> b.value }

This is going to translate into something like

fac_list.sort { |a,b|
facility1.value <=> ‘Float’.value }

As far as I can tell, strings don’t have a “value” method, and it’s
that your facility objects do either, which is probably why you’re
the error.

Most likely, your facility object has a “name” or “title” field, so that
you want to do a meaningful comparison you’d have to use something like.

fac_list.sort { |a,b| <=> }

Which also means that your “Float” and “Main Office” strings will have
respond to the “name” message. Because of this, it might make sense to
two facility object class variables to your model corresponding to
and “Main Office”, especially if you’re going to be using those objects
elsewhere. It might also make sense to put your sorting mechanism in
model as well.

I hope this helps!



@facility = Facility.find(:all, :select => ‘name’)
=> [#<Facility:0xb7a8fb28 @attributes={“name”=>“Summerhill”}>, …

@facility = @facility + [:name => “Main Office”]
=> [#<Facility:0xb7a8fb28 @attributes={“name”=>“Summerhill”}>, …,
{:name=>“Main Office”}]

but this of course doesn’t have the ‘Facility’ class, so I need to add
it to the Facility class and I can’t figure out how that is done.



It should be easy to create Facility objects in the controller, using
something like

@main_office = => “Main Office”)

In the model, I think you could do something like

def self.main_office
@main_office = @main_office || => “Main Office”)

I’m not sure if those "self"s need to be there, but that’s the idea



OK - thanks for the help Daniel…I had to split it into steps but I got



Well, I’m trying to use a model other than self.

@facility = Facility.find(:all, :select => ‘name’) || => ‘Main Office’)

This doesn’t work (in console), ‘Main Office’ isn’t added to the array.

@facility = Facility.find(:all, :select => ‘name’)
=> [#<Facility:0xb790d548 @attributes={“name”=>“Summerhill”}>,

@home_office =
=> #<Facility:0xb790800c @attributes={“fax_number”=>nil, “city”=>nil,
“name”=>nil, “beds_a”=>nil, “zip_code_plus_4”=>nil, “pltype”=>nil,
“beds_m”=>nil, “phone_number”=>nil, “beds_f”=>nil, “address”=>nil,
“state”=>nil}, @new_record=true> = ‘Main Office’
=> “Main Office”

=> #<Facility:0xb790800c @attributes={“fax_number”=>nil, “city”=>nil,
“name”=>“Main Office”, “beds_a”=>nil, “zip_code_plus_4”=>nil,
“pltype”=>nil, “beds_m”=>nil, “phone_number”=>nil, “beds_f”=>nil,
“address”=>nil, “state”=>nil}, @new_record=true>

@facility = @facility + @home_office
TypeError: can’t convert Facility into Array
from (irb):73:in `+’
from (irb):73

This is proving to be difficult to do and I thought it would be easy…