Created
May 22, 2011 19:37
-
-
Save mattdenner/985791 to your computer and use it in GitHub Desktop.
Handy tip for using proxy_owner in named scopes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Thing < ActiveRecord::Base | |
# This named scope is supposed to take in an instance of Owner so that it can be used. Normally people would start | |
# writing `Thing.something(Owner.first).first` but ... | |
named_scope :something, lambda { |owner| | |
# Something here using 'owner' | |
} | |
end | |
class Owner < ActiveRecord::Base | |
# ... this association uses the method_missing behaviour of associations to cheat the appearance of something. In | |
# other words, you can do `Owner.first.things.something.first` which is far nicer. | |
has_many :things do | |
def something | |
super(proxy_owner) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment