【问题标题】:Rails Polymorphic Association - Join elements with similar relationshipsRails 多态关联 - 连接具有相似关系的元素
【发布时间】:2013-08-26 14:19:50
【问题描述】:

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

我想在用户主页上显示每个技能或技能组兴趣的培训,但在我看来,只想遍历受过培训的兴趣(并非所有技能或技能组都受过培训)。

skill.rb 和 skill_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 错误

有什么方法可以进行这种类型的连接吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答!不幸的是,我不再从事这个项目,也无法验证它是否适合我。希望您的回答对其他人有所帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 2016-01-28
    相关资源
    最近更新 更多