Skip to content

Instantly share code, notes, and snippets.

@brian-mann
Created July 28, 2012 21:41
Show Gist options
  • Save brian-mann/3194911 to your computer and use it in GitHub Desktop.
Save brian-mann/3194911 to your computer and use it in GitHub Desktop.
Dynamically call model methods to chain includes
## Customer.rb
def self.references
includes(:account)
end
def self.forms
includes(:forms)
end
def self.conditionally_include
hash = { :references => true, :locations => false, :forms => false }
hash.each do |k, v|
self.send(k) if v
end
end
## in rails console:
Customer.conditionally_include
=> {:references=>true}
but if I call this instead:
Customer.send(:references)
it runs the proper queries on Customer and eagerly loads references
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment