2016-05-10 82 views
0

我有一个应用程序有subtasks,subattributesweightsFindind模型,属于两个模型与has_many通过协会

一个subtask可以有很多通过weightssubattributes,可以有很多weights

一个subattribute可以有很多通过weightssubtasks,可以有很多weights

A weight可以同时属于subtasksubattribute

现在我试图通过收集循环打印重量。但是,我在解决如何获得具体权重时遇到了问题。这是我有什么:

<% @subtasks.each do |subtask| %> 
    <% @subattributes.each do |subattribute| %> 
     <% if subattribute.subtasks.include (subtask) %> 
      #find the weight of that subattribute that's associated with that subtask 
     <% else %> 
     #create a new weight associated with the subattribute and subtask 
     <% end %> 
    <% end %> 
    <% end %> 

任何建议。这是我的第一个rails应用程序,我没有设计后端,因为我个人会认为在子属性中只有一个字段会更容易。任何帮助将不胜感激。

+0

试举你的问题更好的标题。此外,它还会帮助您添加模型的相关行。 – Meier

回答

1

试试这个:

<% @subtasks.each do |subtask| %> 
    <% @subattributes.each do |subattribute| %> 
     <% if subattribute.subtasks.include?(subtask) %> 
     #find the weight of that subattribute that's associated with that subtask 
      <% Weight.where(subattribute_id: subattribute.id,subtask_id: subtask.id).first %> 
     <% else %> 
      #create a new weight associated with the subattribute and subtask 
      <% Weight.create(subattribute_id: subattribute.id,subtask_id: subtask.id) %> 
     <% end %> 
    <% end %> 
<% end %> 
+0

所以问题,我将如何获得一个领域使用的地方?我试过<%Weight.where(subattribute_id:subattribute.id,subtask_id:subtask.is).first.capability%>,但得到了未定义的方法错误。 –

+0

@MeganWoodruff我有一个错字。它应该是'subtask.id'而不是'subtask.is'。更新了上面的答案 – dp7

+0

啊!明白了!非常感谢!我不知道这个地方甚至存在!接受并投票! –