Can this be written cleaner?

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

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

@friend_blogs = Array.new

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

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

thanks

I would try combining the two searches, either by writing the sql
yourself
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

WW

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

Does something like this work for you?

@friends =
Favorite.find_all_by_user_id_and_favorable_type(current_user.id,
‘User’)

@friend_blogs = @friends.blog_posts.find(
:all,
: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’ " , current_user.id)

WW

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

///ark