Skip to content

Instantly share code, notes, and snippets.

@phinze
Created November 2, 2009 18:58
Show Gist options
  • Save phinze/224343 to your computer and use it in GitHub Desktop.
Save phinze/224343 to your computer and use it in GitHub Desktop.
# <rbriggs> is there any way to put conditions with regards to the source object in
# a has_many? for example, I want so say foo.bars and have the bars
# association look for bars with an updated_at <= foo.updated_at (note: I
# know this can be done with a named_scope in bars.class - but I need this
# to be used in as a source association in a :through)
class Foo < ActiveRecord::Base
has_many :bars
def bars_before_me
bars.updated_before(self.updated_at)
end
end
class Bar < ActiveRecord::Base
belongs_to :bar
named_scope :updated_before lambda { |t| :conditions => ["updated_at < ?" t] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment