ActiveRecord can't know that proposal.fee_summaries is reverse association from fee_summary.proposal. It's because you can define your own association name, have additional constraints on it, etc. - to automatically derive which associations are reverse of which would be insanely hard, if not impossible. So even for most straightforward cases you need to tell it explicitly via inverse_of
option on the association declaration. Here's an example for a simple direct association:
class Proposal < ActiveRecord::Base
has_many :proposal_sections, :inverse_of => :proposal
end
class ProposalSection < ActiveRecord::Base
belongs_to :proposal, :inverse_of => :proposal_sections
end
2.0.0-p353 :001 > proposal = Proposal.new
=> #<Proposal id: nil, created_at: nil, updated_at: nil>
2.0.0-p353 :002 > section = proposal.proposal_sections.new
=> #<ProposalSection id: nil, proposal_id: nil, created_at: nil, updated_at: nil>
2.0.0-p353 :003 > section.proposal
=> #<Proposal id: nil, created_at: nil, updated_at: nil>
Unfortunately, inverse_of
does not support indirect (through
) and polymorphic assocations. So in your case there's no easy way to make it work. The only workaround I see is to persist the records (use create
), so AR can just query the relationships by key and return correct results.
Check the docs for more examples and explanations: http://apidock.com/rails/ActiveRecord/Associations/ClassMethods