Skip to content

Instantly share code, notes, and snippets.

@avit
Created March 30, 2010 21:05
Show Gist options
  • Save avit/349592 to your computer and use it in GitHub Desktop.
Save avit/349592 to your computer and use it in GitHub Desktop.
# Ok, this is just a simplified example:
@parent.children.all_passing? # => true or false
# I want a method that returns something based on the collection of
# associated objects. Using association extensions, I can put a
# method on the has_many in parent model:
class Parent < ActiveRecord::Base
has_many :children do
def all_passing?( date )
proxy_target.all?{ |child| child.average_score > 50 }
end
end
end
# However, I think this definition belongs on the child model,
# in case the child model is associated to other models,
# the other associations should also have this method.
# Here it's a simple belongs_to, but is there a way to move the
# above method definition to this side of the association?
class Child < ActiveRecord::Base
belongs_to :parent
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment