Created
July 28, 2012 21:41
-
-
Save brian-mann/3194911 to your computer and use it in GitHub Desktop.
Dynamically call model methods to chain includes
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
## 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