the question below had answer grab associated values of activerecord collection in 1 hit using comment.includes(:user)
. when have multiple associations want grab in 1 go?
rails have activerecord grab needed associations in 1 go?
is best way chain these below customer.includes(:user).includes(:sales).includes(:prices)
or there cleaner way.
furthermore, when doing on loop on index table. can add method on customer.rb
model can call @customers.table_includes
etc , have
def table_includes self.includes(:user).includes(:sales).includes(:prices) end
for record tested above , didn't work because method on collection (yet figure out how this).
in answering this, i'm assuming user
, sales
, , prices
associations off of customer
.
instead of chaining, can this:
customer.includes(:user, :sales, :prices)
in terms of creating abstraction this, have couple options.
first, create scope:
class customer < activerecord::base scope :table_includes, -> { includes(:user, :sales, :prices) } end
or if want method, should consider making class-level method instead of instance-level one:
def self.table_includes self.includes(:user, :sales, :prices) end
i consider purpose of creating abstraction though. generic name table_includes
not friendly on long term.