Posted By

gnatware on 02/04/07


Tagged

find associations


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

bbebop
adam_dc
webstic


DRY up frequent finds


 / Published in: Ruby
 

URL: http://www.therailsway.com/2007/1/10/assetsgraphed-part-2

  1. # Always be on the lookout for duplicated code. If you find yourself doing
  2. # asset.incomings.find(:all, :limit => 10) in multiple places, then perhaps you need to
  3. # pull that into a method of its own. Given the use of the magic number "10", it might
  4. # not be a bad idea to do so anyway.
  5.  
  6. class Asset < ActiveRecord::Base
  7. has_many :incomings do
  8. def recent(count=10)
  9. find(:all, :limit => count)
  10. end
  11. end
  12. end
  13.  
  14. # Then, you just have to do @asset.incomings.recent to get the first 10 items.

Report this snippet  

You need to login to post a comment.