On Apr 20, 2007, at 12:29 PM, Neeraj Kumar wrote:> I have a Person model and a Phone model.
>
> A person can have any number of phones.
>
> A phone has two attributes: type and number.
>
> A phone type could be any of the followings:
> Home
> Work
> Mobile
> Fax
> Other
>
>
> A person can have multiple records of home phones or any other
> phone type.
>
> While displaying the phones this is what I am doing.
>
> <% @phones.group_by(&:type).sort.each do |type, phones| %>
> <%= render :partial => ''phone, :collection => @phones
%> <br />
> <% end %>
>
>
> But the problem is that it is returning phone numebers in the
> following order:
> Fax
> Home
> Mobile
> Office
> Work
>
> I want the phone numbers to be displayed in the following order:
> Work
> Mobile
> Home
> Fax
> Other
>
> What''s the best way to achieve that?
"Best", Hmm, don''t know, but here''s what
I''ve done for similar
reasons (mapped into your domain):
class Phone
SORT_ORDER = %w[ Work Mobile Home Fax Other ]
def self.pos phone_type=''Other''
SORT_ORDER.index(phone_type)
end
end
<% @phones.group_by(&:type).sort_by {|k,v| Phone.pos(k) }.each do |
type, phones| %>
<%= render :partial => ''phone'', :collection => phones
%> <br />
<% end %>
(I fixed what looked like two typos in the render line as well:
missing '' and the collection from the block variable.)
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---