# Select list

**URL:** <https://www.ruby-forum.com/t/select-list/94884>\
**Category:** Rails\
**Created:** [May 17, 2006, 2:49am UTC](https://www.ruby-forum.com/t/select-list/94884 "2006-05-17T02:49:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 17, 2006, 2:49am UTC](https://www.ruby-forum.com/t/select-list/94884/1 "2006-05-17T02:49:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 17, 2006, 7:32am UTC](https://www.ruby-forum.com/t/select-list/94884/2 "2006-05-17T07:32:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 17, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/select-list/94884/3 "2006-05-17T18:12:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 17, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/select-list/94884/4 "2006-05-17T20:07:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 17, 2006, 9:19pm UTC](https://www.ruby-forum.com/t/select-list/94884/5 "2006-05-17T21:19:00Z")

</div>

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

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [May 17, 2006, 8:44pm UTC](https://www.ruby-forum.com/t/select-list/94884/6 "2006-05-17T20:44:22Z")

</div>

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
