rubyonrailsin

A Ruby and Rails talk

Friday, April 2, 2010


[Rails] Re: Best way of sorting/grouping objects

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

By any chance do you have a model named PageType? If you, you could
set up a relationship between PageType and Page:

page.rb:
belongs_to :page_type

page_type.rb:
has_many :pages

Then display it with:

your_controller.rb:
@page_types = PageType.all

your_view.html.haml (or .erb, etc.)
- @page_types.each do |page_type|
%h1= page_type.name
%ul
= list_of page_type.pages do |page|
= page.name

If you don't already have a PageType model, or don't want to introduce
another model, you could probably use:

@page_types = Page.all(:order => 'page_type', :group => 'page_type')

and then play around with the result of that. (At least I think that's
correct… haha, sorry if it's not)

Good luck,
Angelo

On Apr 2, 7:31 am, Brent <wejrow...@gmail.com> wrote:
> That could work. But I need it to order by page_order also.  I would
> like to group them into 3 groups then sort them by that. In the end I
> want it to look something like this in html...
>
> TYPE ONE
> Page 1
> Page 2
>
> TYPE TWO
> Page 1
> Page 2
>
> Before I was doing something like this (I had an array of each page
> type called page_types):
>
> <% page_types.each do |t| %>
>     <%= t %>'s
>    <% Page.find(:all, :conditions=>{:page_type=>t}, :order=>position
> %>
>      etc.
>   <% end %>
> <% end %>

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive