DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Sharing Has_many Extensions

07.23.2008
| 3418 views |
  • submit to reddit
        Sometimes you extend an ActiveRecord association this way:

has_many :things do
  def active
    find :all, :conditions => ['active = ?', true]
  end
end

You can share the same extensions using a lambda:


extensions = lambda {
  def active
    find :all, :conditions => ['active = ?', true]
  end
}

has_many :things, &extensions
has_many :more_things, &extensions