2016-05-10 1 views
0

subtasks,subattributes、およびweightsのアプリケーションがあります。アソシエーションを介してhas_manyを持つ2つのモデルに属しているFindindモデル

subtaskweightsを通じて多くのsubattributesを持つことができ、weights多くを持つことができます。

subattributeweightsを通じて多くのsubtasksを持つことができ、weights多くを持つことができます。

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 %> 

何か提案があります。これは私の最初のレールアプリであり、私は個人的には重量をサブ属性のフィールドにする方が簡単だと思っていたので、バックエンドを設計していませんでした。どんな助けでも大歓迎です。

+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.is'の代わりに' subtask.id'にする必要があります。上記の答えを更新しました – dp7

+0

ああ、それを得ました!本当にありがとう!私はこれがどこにあったのか分からなかった!受け入れて投票してください! –

関連する問題