Skip to content

Instantly share code, notes, and snippets.

@mattdenner
Created May 22, 2011 19:37
Show Gist options
  • Save mattdenner/985791 to your computer and use it in GitHub Desktop.
Save mattdenner/985791 to your computer and use it in GitHub Desktop.
Handy tip for using proxy_owner in named scopes
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