Topic: Searching through a find(:all) result

So I currently have a controller that makes way too many SQL calls, I'm sure. The first is the typical Posts.find(:all) then there are others using the same resource to get specific Posts based on :conditions. Is there a way to just search through that Posts.find(:all) result with Ruby methods to pull sub-groups from it, ultimately cutting out those extra SQL calls.

I'm at work and the project is on my home computer, so unfortunately I don't have any code examples.

Thanks in advance.

Re: Searching through a find(:all) result

the collection is basically an array, so your friends are #select (for searching for many results) and #detect for searching for one/the first result) to search an array:

posts = Post.find(:all)
@published_posts = @posts.select {|p| p.published == 1}
@todays_posts = @posts.select {|p| p.published == 1 && posts.created_on == Date.today}
....

The conditions can be anything you could use in a normal if statement.

#select returns an (maybe empty) array, #detect returns a post or nil.

Re: Searching through a find(:all) result

Awesome, that answers it perfectly. I figured the result was an array but was unsure of the ruby methods for searching through. Thanks a lot Duplex!