Can this be written cleaner?

i brute force this, is there a cleaner way>??

@friend_ids = Favorite.find(:all, :conditions => [“user_id =
(#{}) and favorable_type = ‘User’”])

@friend_blogs =

@friend_ids.each do |x|
@friend_blogs << BlogPost.find(:all, :conditions =>[“user_id = ?”,
x.favorable_id],:order => “created_at DESC”, :limit => 5)

if like to get only the latest 5 posts of all the users that is friends
with current user.


I would try combining the two searches, either by writing the sql
or subtituing user in(?) and doing a find there. You could do it with
the :include tag
but I haven’t used that yet, if you need to google search its called
eager loading


On Nov 30, 8:19 am, Koloa P. [email protected]

Does something like this work for you?

@friends =

@friend_blogs = @friends.blog_posts.find(
:order => “created_at DESC”,
:limit => 5)

On Nov 30, 8:19 am, Koloa P. [email protected]

I dont like the first query, it would fall into method missing and
that takes alot of overhead to create it. I would instead write it as.

@friends = Favorite.find(:all, :conditions => ["user_id = ? and
favorable_type = ‘User’ " ,


The overhead of method_missing would be greatly outweighed by the cost
of the database query. Premature optimization is the root of all evil.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs