viewでaffiliationがnilになります。
リレーションの貼り方が間違っていますか?
ruby
1# index.html.slim 2<%= order.affiliation.name %>
order.affiliationで発行されるSQL
SQL
1SELECT "affiliations".* FROM "affiliations" INNER JOIN "branches" ON "affiliations"."id" = "branches"."affiliation_id" WHERE "affiliations"."deleted_at" IS NULL AND "branches"."order_id" = $1 LIMIT $2 [["order_id", 8161], ["LIMIT", 1]]
試してこと
このように中間テーブルから辿ると表示することができました。
ruby
1<%= order.branch&.affiliation&.name %>
リレーション
ruby
1class Order < ApplicationRecord 2 has_one :branch, dependent: :destroy 3 has_one :affiliation, through: :branch 4end
ruby
1class Branch < ApplicationRecord 2 belongs_to :order 3 belongs_to :affiliation 4end
ruby
1class Affiliation < ApplicationRecord 2 has_many :branches, dependent: :destroy 3 has_many :orders, through: :branches 4end
Branchは中間テーブルですよね?Branchのカラムはどういう構成でしょうか?
回答3件
あなたの回答
tips
プレビュー