hi everyone, in this example I'm going to use a simple has_many :through association.
Browsing Rails' source I noticed that assigning some objects to a collection when the record is new and then saving, I can pass every kind of object, which passes validate_associated or not:
@test = Report.new({"document_id"=>nil, "created_at"=>nil, "updated_at"=>nil, "when"=>nil})
=> #<Report id: nil, document_id: nil, when: nil, created_at: nil, updated_at: nil>
@test.reasons << Reason.find_or_create_by_content("pr") # invalid records mass fest, validation will occur on save
=> [#<Reason id: nil, content: "pr", created_at: nil, updated_at:
]
@test.save
=> false # all green
On the contrary when the object (owner) is already in place, it bombs out with an exception.
@test = Report.first
=> #<Report id: 1, document_id: 1, ...
@test.reasons << Reason.find_or_create_by_content("pr") # ACK
ActiveRecord::RecordInvalid: Validation failed: Content is too short (minimum is 3 characters)
Nothing strange, I'm sure that's the correct behaviour, what I would like to ask is if there's some chance to "defer" the assignment, i.e. acknowledge @test that he will have some reasons but do not raise if some of them are invalid, leaving the job to update_attributes in controller.
Would be a patch useful, if currently there are no ways, regarding this matter?