1

I have models Order and OrderItem. I'd like to autoload the Items once an order is loaded. Something like this:

class Order < ActiveRecord::Base
  has_many :order_items, :include => true
end

or

class Order < ActiveRecord::Base
  include :order_items
  has_many: order_items
end

I'm sure that I have seen something like that but I can't find it anymore.

4

1 回答 1

0

您可以像这样使用 default_scope:

class Order < ActiveRecord::Base
  has_many :order_items

  default_scope includes(:order_items)
end

:include定义中has_many :order_items用于预先加载二阶关联,即定义在OrderItems.

于 2013-01-20T14:54:46.813 回答