inverse_of and #save called multiple times

24 views
Skip to first unread message

Michel Pigassou

unread,
May 31, 2013, 1:39:30 PM5/31/13
to rubyonra...@googlegroups.com
Hi.

I created an app to illustrate my problem: https://github.com/Dagnan/rails_inverse_of

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.

Michel Pigassou

unread,
Jun 3, 2013, 1:55:29 AM6/3/13
to rubyonra...@googlegroups.com
Help?

Rick

unread,
Jun 3, 2013, 11:13:26 AM6/3/13
to rubyonra...@googlegroups.com
I cannot duplicate your error running your github example.  Here's what I see:

/Dagnan/rails_inverse_of 656 > rails --version
Rails 3.2.13
/Dagnan/rails_inverse_of 657 > ruby --version
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0]
/Dagnan/rails_inverse_of 658 >  ruby -Itest test/unit/campaign_test.rb
Run options:

# Running tests:

[1/1] CampaignTest#test_create_a_campaign_with_recurrenceHere
Here
Finished tests in 0.195217s, 5.1225 tests/s, 0.0000 assertions/s.
1 tests, 0 assertions, 0 failures, 0 errors, 0 skips

ruby -v: ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0]
/Dagnan/rails_inverse_of 659 >

Rick

unread,
Jun 3, 2013, 11:17:37 AM6/3/13
to rubyonra...@googlegroups.com
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.

Michel Pigassou

unread,
Jun 3, 2013, 11:57:20 AM6/3/13
to rubyonra...@googlegroups.com
Hmm thanks. Is it worth it to report this to the Rails team?

Rick

unread,
Jun 3, 2013, 6:34:34 PM6/3/13
to rubyonra...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages