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’]
end

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
code…

<%= 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?

Craig

Hi Craig,

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

[ facility1,
facility2,
facility3,
‘Float’,
'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
unlikely
that your facility objects do either, which is probably why you’re
getting
the error.

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

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

Which also means that your “Float” and “Main Office” strings will have
to
respond to the “name” message. Because of this, it might make sense to
add
two facility object class variables to your model corresponding to
“Float”
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
the
model as well.

I hope this helps!

Daniel

@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.

Craig

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

@main_office = Facility.new(:name => “Main Office”)

In the model, I think you could do something like

def self.main_office
@main_office = @main_office || self.new(:name => “Main Office”)
End

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

Daniel

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

Craig

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

@facility = Facility.find(:all, :select => ‘name’) ||
Facility.new(: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”}>,
…snip…

@home_office = Facility.new
=> #<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>

@home_office.name = ‘Main Office’
=> “Main Office”

@home_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…

Thanks

Craig