Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_attribute ignores autosave relations #25503

Closed
Tietew opened this issue Jun 24, 2016 · 5 comments

Comments

Projects
None yet
6 participants
@Tietew
Copy link

commented Jun 24, 2016

Steps to reproduce

require 'bundler/setup'
require 'active_record'
require 'sqlite3'
require 'minitest/autorun'

ActiveRecord::Base.establish_connection 'sqlite3::memory:'
ActiveRecord::Schema.define do
  create_table :people do |t|
    t.string :name
    t.timestamps null: false
  end
  create_table :emails do |t|
    t.belongs_to :person
    t.string :addr
  end
end

class Person < ActiveRecord::Base
  has_one :email, dependent: :destroy, autosave: true
  delegate :addr, :addr=, to: :email
end
class Email < ActiveRecord::Base
  belongs_to :person, touch: true
end

class BugTest < Minitest::Test
  def test_touch
    person = Person.create(name: 'Test')
    person.create_email(addr: 'test1@example.com')

    assert_equal 'test1@example.com', Person.find(person.id).addr # Pass

    # Expected to save associated Email model
    person.update_attribute(:addr, 'test2@example.com')

    assert_equal 'test2@example.com', Person.find(person.id).addr # FAIL HERE
  end
end

Expected behavior (Rails 4.2.6)

The test passed.
update_attribute saves associated Email model.

Actual behavior (Rails 5.0.0.rc2)

The test failed.
Nothing is saved.

Is it intentional?

System configuration

Rails version: Rails 5.0.0.rc2

Ruby version: Ruby 2.3.1

@alexcameron89

This comment has been minimized.

Copy link
Member

commented Jun 24, 2016

I ran a git bisect, and it looks like this behavior was added in 0fcd4cf.

@maclover7

This comment has been minimized.

Copy link
Member

commented Jun 24, 2016

Hmm, I'm not sure if this is supported behavior...

@rafaelfranca

This comment has been minimized.

Copy link
Member

commented Jun 24, 2016

I'd say yes. It changed the behavior as consequence to fix an issue but it was never expected to remove support to this.

@rafaelfranca rafaelfranca added this to the 5.0.1 milestone Jun 24, 2016

@rafaelfranca rafaelfranca removed this from the 5.0.1 milestone Jun 24, 2016

@uaihebert

This comment has been minimized.

Copy link

commented Oct 2, 2016

I've found the problem.

The changed? method was not checking for an association update, but only for attributes update.

I need some ideas about the best way to check if an association have been updated. I needed to do some ifs because some tests were broken if I ran in group, but when I run individually it works.

Thanks

@kamipo

This comment has been minimized.

Copy link
Member

commented Dec 25, 2017

Fixed by #27780.

@kamipo kamipo closed this Dec 25, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.