【发布时间】:2011-01-24 07:18:15
【问题描述】:
使用多态关联时,是否可以在仅存在于某些类型中的子模型上运行包含?
例子:
class Container
belongs_to :contents, :polymorphic => true
end
class Food
has_one :container
belongs_to :expiration
end
class Things
has_one :container
end
在视图中,我想做一些类似的事情:
<% c = Containers.all %>
<% if c.class == Food %>
<%= food.expiration %>
<% end %>
因此,我想在加载 c 时急切加载到期时间,因为我知道我将需要它们中的每一个。有什么办法吗?仅仅定义一个常规的 :include 就会出错,因为并非所有封闭的类型都有子模型到期。
【问题讨论】:
标签: ruby-on-rails include polymorphic-associations eager-loading rails-activerecord