Topic: what's the proper way to do this?

i have different partials that display different subsets of data on pages, quite often as a partial on another controller generated page.

say i have comments against an item, sometimes i want to see all the comments, others just a subset.

in one partial, for example, i want to display all comments that have been approved (determined by a non null approved_at date)

in my view, i'm calling the partial against a collection:

<%= render :partial => 'comments/approved_comments', :collection => @page.approved_comments %>

the partial obviously provides the layout, in this case it is a list item with some gubbins.

the 'approved_comments' method is defined in my model:

  def approved_comments { |comment| !comment.approved_at.nil? }

now, this works, but i know it's not the right way as i remember seeing samples for what i am trying to achieve, but can't find them when i search.

any help appreciated...

Re: what's the proper way to do this?

Looks like you're doing it correct for the most part. The only thing I'd change is how you select the approved comments. You can do it in one database call like this:

def approved_comments
  comments.find(:all, :conditions => 'approved_at IS NOT NULL')

This way if you are only fetching the approved comments it won't have to fetch all of the comments from the database.

Were you looking for something else?

Railscasts - Free Ruby on Rails Screencasts

Re: what's the proper way to do this?

thanks ryan, i just wanted to know the way the more experienced folk would do it - i'll modify it to be a find with conditions.