2017-10-25 6 views
0

私は以下のhas_many関連を持っています。レールにhas_manyが表示されているだけでテーブル名が表示されます

class Indicator < ApplicationRecord 
    belongs_to :statement, required: false 
    belongs_to :identity, required: false 
    belongs_to :phase, required: false 
end 

class Statement < ApplicationRecord 
    belongs_to :student, required: false 
    has_many :indicators 
    has_many :identities, through: :indicators 
    accepts_nested_attributes_for :identities 
end 

class Identity < ApplicationRecord 
    has_many :indicators 
    has_many :statements, through: :indicators 
    accepts_nested_attributes_for :statements 
    has_many :phases 
end 

class Phase < ApplicationRecord 
    has_many :indicators 
    belongs_to :identity, required: false 
end 

私はshow.html.erbというステートメントで結果を呼び出すことを検討しています。下のコードの<%= l.phases.name %>部分には、名前の値ではなくテーブル名である 'Phase'が表示されます。

<table> 
<th>Description</th> 
<th>Phase</th> 
<% @statement.identities.each do |l| %> 
    <tr> 
    <td><%= l.description %></td> 
    <td><%= l.phases.name %></td> 
    </tr> 
    <% end %> 
</table> 

これは、インジケータ表が設定されているか:それはあなたがループして各フェーズの名前が表示される必要があり、多くの段階がありますので

| id | statement_id | identity_id | phase_id | 
| 1 |  2  |  1  | 3  | 
| 2 |  2  |  2  | 2  | 
| 3 |  3  |  1  | 1  | 
| 4 |  3  |  2  | 1  | 

答えて

1

<% l.phases.each do |p| %> 
<%= p.name %> 
<% end %> 
+0

ありがとう@shannon。私はそれを試みましたが、関連するすべてのidentity_idsをリストしているようです。私は私の団体が間違っているかもしれないと思う。上記の質問を更新して、インジケータテーブルの構造を示しました。 – DollarChills

0

あなたの関連は間違っています。 ID has_many :phasesとフェーズbelongs_to :identity以降、スルーオプションを指定する理由はありません。そうすると、Identityの特定のインスタンスの各フェーズの名前を取得するために、

<% @statement.identities.each do |l| %> 
    <tr> 
    <td><%= l.description %></td> 
    <td><%= l.phases.pluck(:name) %></td> 
    </tr> 
    <% end %> 

を実行します。

+0

ありがとうMisterCal。上記のスチルは私に配列/ハッシュを与えます。 # DollarChills

+0

実際のオブジェクトではなく値を返すselectの代わりにpluck(:name)を試してください。上に編集 – MisterCal

+0

いいえ、運がありません。これは、キーIDを段階的に探しています。 **そのような列:phases.identity_id:SELECT "段階" "名前" FROM "段階" WHERE "段階"。 "identity_id" =?**。私は上記のインジケータテーブルで一致するように探しています。私は自分自身を正しく告白したと思う。 – DollarChills

関連する問題