Rails Polymorphic Association - 加入具有相似关系的元素

时间:2013-08-26 14:19:50

标签: ruby-on-rails ruby-on-rails-3 polymorphic-associations

我有一个系统,可以在我的网站上显示技能和(基本上)技能组的培训资源。用户可以表达对存储在多态表interests中的技能或技能分组的兴趣。

我希望在用户主页上显示每个技能或技能组感兴趣的培训,但在我看来,只想迭代已经培训的兴趣(并非所有技能或技能组都有培训)。

skill.rbskill_group.rb模型都定义了培训资源关系。

_interests.html.erb

# Currently iterate through all interests
<% @user.interests.each do |interest| %> 
  <% interest_element = interest.interest_element %>
  <% if interest_element.training_resources.size > 0 %>
    # More rendering code here...
  <% end %>

<% end %>

interest.rb

class Interest < ActiveRecord::Base
  belongs_to :user
  belongs_to :interest_element, :polymorphic => true
end

理想情况下,我想通过以下方式迭代一系列有限的兴趣:

@user.interests.joins(:interest_element => :training_resources).each do |interest|

但我收到Can not eagerly load the polymorphic association :interest_element错误

我有什么方法可以做这种类型的加入吗?

1 个答案:

答案 0 :(得分:0)

试试@ user.interests.includes(:interest_elements) 在轨道4中工作。

相关问题