Dziennik zdarzeń może być albo fakturowany, albo nie, a faktura zawiera wiele etykiet TimeLog. Nasza baza danych nie może mieć zerowych kluczy obcych, więc używamy modelu łączenia. Kod:has_one: through nie udostępnia funkcji 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 działa dobrze, ale TimeLog.first.build_invoice daje
NoMethodError: undefined method `build_invoice' for #<TimeLog:0x4acd588>
Czy has_one nie powinien sprawić, że metoda build_association dostępne?
Aktualizacja:
Zrobiłem przykładowy repo to pytanie: build_assocation_test. Widzi ten problem, sklonować repo zainstalować pakiet, uruchom migracje (lub załadować schematu), a następnie w konsoli szyn:
Invoice.create
Invoice.first.time_logs.build
TimeLog.create
TimeLog.first.build_invoice
Czy możesz potwierdzić, że masz co najmniej 1 Timelog w swoim DB? Lub opublikuj pełny błąd NoMethodError. –