I have a model with a belongs_to, and the other with a has_one. So far
so
good.
When I configure the option inverse_of on both model and I perform a
simple #save on the main object, it is actually saved two times (once saved and
then updated).
Is it an expected behavior?
A way to avoid this problem would be not to use inverse_of, or to have
“autovalidate: false” in the second model (Recurrence in my example) for
the has_one association.
I just noticed that your error does, in fact, appear in my output.
However, if I run inside of the rails console I don’t see the redundant
“Here”. i.e.:
/Dagnan/rails_inverse_of 659 > rails c
Loading development environment (Rails 3.2.13)
irb(main):001:0> c = Campaign.new
=> #<Campaign id: nil, created_at: nil, updated_at: nil, recurrence_id:
nil>
irb(main):002:0> c.recurrence = Recurrence.new
=> #<Recurrence id: nil, created_at: nil, updated_at: nil>
irb(main):003:0> c.save!
(0.1ms) begin transaction
SQL (5.1ms) INSERT INTO “recurrences” (“created_at”, “updated_at”)
VALUES (?, ?) [[“created_at”, Mon, 03 Jun 2013 15:15:11 UTC +00:00],
[“updated_at”, Mon, 03 Jun 2013 15:15:11 UTC +00:00]]
Here
SQL (0.5ms) INSERT INTO “campaigns” (“created_at”, “recurrence_id”,
“updated_at”) VALUES (?, ?, ?) [[“created_at”, Mon, 03 Jun 2013
15:15:11
UTC +00:00], [“recurrence_id”, 1], [“updated_at”, Mon, 03 Jun 2013
15:15:11
UTC +00:00]]
Here
(49.1ms) commit transaction
=> true
irb(main):004:0> c
=> #<Campaign id: 1, created_at: “2013-06-03 15:15:11”, updated_at:
“2013-06-03 15:15:11”, recurrence_id: 1>
irb(main):005:0>
My guess is it’s a “test mode” artifact of some kind.
might be worth spending some time to see if you can figure out where the
extra “Here” comes from. i didn’t check the log to see if there was
actually a redundant save of the record. could be an opportunity for
you
to plumb the mysteries of testing. think of what it did for heisenberg.