Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

has_and_belongs_to_many autosave: true, dont save child records #13923

Closed
maurogeorge opened this Issue · 9 comments

4 participants

Mauro George Rafael Mendonça França Yves Senn Girish Sonawane
Mauro George

Guys,

I was working on shoulda-matchers to make his works with the rails 4.1.0.beta1. I guess the has_and_belongs_to_many with autosave: true is broken.

I show some code. First the models

class Person < ActiveRecord::Base
  has_and_belongs_to_many :relatives, :autosave => true
end

class Relative < ActiveRecord::Base
end

class PeopleRelative  < ActiveRecord::Base
end

In rails 3.2:

> p = Person.create!
=> #<Person id: 1, name: nil, created_at: "2014-02-02 14:01:07", updated_at: "2014-02-02 14:01:07">
> r = Relative.create!
=> #<Relative id: 1, adopted: nil, created_at: "2014-02-02 14:01:07", updated_at: "2014-02-02 14:01:07">
> p.relatives << r
=> [#<Relative id: 1, adopted: nil, created_at: "2014-02-02 14:01:07", updated_at: "2014-02-02 14:01:07">]
> p.name = 'John Doe'
=> "John Doe"
> r.adopted = false
=> false
> p.save!
=> true
> p.reload
=> #<Person id: 1, name: "John Doe", created_at: "2014-02-02 14:01:07", updated_at: "2014-02-02 14:01:07">
> r.reload
=> #<Relative id: 1, adopted: false, created_at: "2014-02-02 14:01:07", updated_at: "2014-02-02 14:01:07">

Works as expected on rails 3.2, when I save Person, its saves the Relative, the adopted change from nil to false.

In Rails 4.1.0.beta1

> p = Person.create!
=> #<Person id: 1, name: nil, created_at: "2014-02-02 14:02:04", updated_at: "2014-02-02 14:02:04">
> r = Relative.create!
=> #<Relative id: 1, adopted: nil, created_at: "2014-02-02 14:02:04", updated_at: "2014-02-02 14:02:04">
> p.relatives << r
=> [#<Relative id: 1, adopted: nil, created_at: "2014-02-02 14:02:04", updated_at: "2014-02-02 14:02:04">]
> p.name = 'John Doe'
=> "John Doe"
> r.adopted = false
=> false
> p.save!
=> true
> p.reload
=> #<Person id: 1, name: "John Doe", created_at: "2014-02-02 14:02:04", updated_at: "2014-02-02 14:02:04">
> r.reload
=> #<Relative id: 1, adopted: nil, created_at: "2014-02-02 14:02:04", updated_at: "2014-02-02 14:02:04">

On rails 4.1.0.beta1, when I save Person, its dont save the Relative, the adopted keeps nil.

Mauro George maurogeorge referenced this issue in thoughtbot/shoulda-matchers
Merged

Add support to Rails 4.1 #434

Yves Senn
Owner

I can confirm this is a regression from 4-0-stable. It's caused by the fact that now habtm associations are implemented in terms of hm:t. The :autosave option is lost in that translation. I'll take a look and write a patch.

Yves Senn senny self-assigned this
Yves Senn senny added the regression label
Mauro George

Great! Thanks guys.

Yves Senn senny closed this in 584a464
Yves Senn
Owner

The error was in fact the refactoring to use hm:t for habtm associations. Our tests didn't catch the regression because the relevant test-case used an association that was using accepts_nested_attributes_for. That macro enables the :autosave flag and kept working after the refactoring :sweat:

I added another test-case with a raw association to prevent against further regressions.

@maurogeorge thanks for reporting :yellow_heart:

Girish Sonawane

@senny you beat me by a couple of hours... this was supposed to be my first rails commit! Although your solution is more elegant than mine! :)

Yves Senn
Owner

@girishso sorry to hear, please respond on tickets you are planning to work on. Also share your investigation results so that the invested time can be used to move the issues forward.

Mauro George

@senny thanks a lot :green_heart:

Girish Sonawane

Thank you @senny will keep that in mind!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.