TimeLogは請求書にすることができますが、Invoiceには多くのTimeLogが含まれます。私たちのデータベースにはヌル可能な外部キーはありません。そのため、結合モデルを使用しています。コード:has_one:throughはbuild_associationを提供しません
class TimeLog < ActiveRecord::Base
has_one :invoices_time_logs
has_one :invoice, through: :invoices_time_logs
end
class Invoice < ActiveRecord::Base
has_many :invoices_time_logss
has_many :time_logs, through: :invoices_time_logss
end
class InvoicesTimeLogs
belongs_to :invoice
belongs_to :time_log
end
Invoice.first.time_logs.buildが正常に動作しますが、TimeLog.first.build_invoiceはbuild_associationメソッドを利用できるようになってhas_oneのではないです
を与えますか?NoMethodError: undefined method `build_invoice' for #<TimeLog:0x4acd588>
私はこの質問のためのサンプルのレポを作りました。 、問題を参照してくださいレポのクローンを作成し、バンドルをインストールし、移行を実行します(またはスキーマをロード)した後、レールコンソールにするには、次の
Invoice.create
Invoice.first.time_logs.build
TimeLog.create
TimeLog.first.build_invoice
DBに1つ以上のTimelogがあることを確認できますか?または、完全なNoMethodErrorエラーを送信します。 –