Forum: Ruby on Rails Select Collection with Conditions - May Help You...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fa06489a97b0efe3c3b7d249982878d3?d=identicon&s=25 David Hughes (divotdave)
on 2006-04-12 08:05
Hey All:

Thought I'd pass this along...let me know if you have a better, more
elegant way.

Still new to Rails, but was fiddling around with the Select Helper
trying to return an option collection that was filtered (in other words,
I didn't want every item from the collection, just the ones that met a
certain criteria).

I used find_all_by_ to do it and it looks like this in my view:

<% select 'deal', 'supplier', Client.find_all_by_is_supplier(true,
:order => 'name').collect { |c| [c.name, c.id] } %>

Note the sort order as well. Hope this helps someone...

Cheers,
David
This topic is locked and can not be replied to.