# Can this be written cleaner?

**URL:** <https://www.ruby-forum.com/t/can-this-be-written-cleaner/121745>\
**Category:** Rails\
**Created:** [November 30, 2007, 2:19pm UTC](https://www.ruby-forum.com/t/can-this-be-written-cleaner/121745 "2007-11-30T14:19:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mauibbq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mauibbq](https://www.ruby-forum.com/u/mauibbq)\
**Post date:** [November 30, 2007, 2:19pm UTC](https://www.ruby-forum.com/t/can-this-be-written-cleaner/121745/1 "2007-11-30T14:19:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mauibbq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mauibbq](https://www.ruby-forum.com/u/mauibbq)\
**Post date:** [November 30, 2007, 3:36pm UTC](https://www.ruby-forum.com/t/can-this-be-written-cleaner/121745/2 "2007-11-30T15:36:28Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mauibbq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mauibbq](https://www.ruby-forum.com/u/mauibbq)\
**Post date:** [November 30, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/can-this-be-written-cleaner/121745/3 "2007-11-30T21:44:35Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mauibbq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mauibbq](https://www.ruby-forum.com/u/mauibbq)\
**Post date:** [December 6, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/can-this-be-written-cleaner/121745/4 "2007-12-06T16:05:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mauibbq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c37758/32.png) [@mauibbq](https://www.ruby-forum.com/u/mauibbq)\
**Post date:** [December 6, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/can-this-be-written-cleaner/121745/5 "2007-12-06T17:59:23Z")

</div>

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
