DRY up frequent finds


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  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.

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.