views:

50

answers:

1

Hi Everyone,

I have a select list in my model which lists a persons name with their employers name:

<li>Case Handler Name<span><%= f.select :person_id, Person.all.collect { |x| [x.name_and_company, x.id] } %></span></li>  

def name_and_company
return "#{personname} (#{company})"
end

Is it possible to force the select list to output in alphabetical order?

I am assuming I would put an order tag in there...somewhere?

(:order => 'personname DESC')

Thanks,

Danny

+1  A: 

You can do it like this

# controller
@people = Person.order_by('personname ASC').collect {|x| [x.name_and_company, x.id] }

# model
named_scope :order_by, lambda { |o| {:order => o} }  

# view
<%= f.select :person_id, @people %>
j.
Hi J. If I am using the above in the Kase view, should it be in the people controller or the kase controller? Also, would the controller section of your code go within the def create block? Thanks.
dannymcc
If the view corresponds to a Kase's controller action, it should be in this same action on Kase's controller.
j.