Skip to content

Instantly share code, notes, and snippets.

@marcamillion
Forked from anonymous/gist:4316571
Created December 17, 2012 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcamillion/4316662 to your computer and use it in GitHub Desktop.
Save marcamillion/4316662 to your computer and use it in GitHub Desktop.
This allows you to do something like this: Say Advertiser is an assoc on banner, you could do something like: Advertiser.first.featured_banners And if you later add a banner type named 'Awesome' you could immediately call Advertiser.first.awesome_banner without writing new code in the model.
#Here's what I'm using. Maybe you could adapt it to your case. I have this on a Message model.
def method_missing(method_name, *args, &block)
if method_name =~ /(.*)_recipients$/
self.correspondences.where('recipient_type = ?', $1.to_s.humanize)
.collect{ |c| c.recipient }.flatten.uniq
else
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment