Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Unexpected side effect of update_attribute implementation change on associations and attributes

#5095

I believe this was the result of 01629d180468049d17a8 as rolling back to the commit before removed the issue.

Having:

class Account < ActiveRecord::Base
  has_many :users
end
class User < ActiveRecord::Base

  belongs_to :account
  
  after_create :create_account
  
  def create_account
    self.update_attribute(:account, Account.create) if self.account_id.blank?
  end
  
end

I now get a sqlite3 error that there is no column 'account' for the Users table. I'm not sure if this was intentional or not. Additionally:

class Task < History

  attr_accessor :complete_note
  
  def complete_note=(complete_note)
    self.body = body + "\n" + complete_note
  end

end
@task.update_attribute(:complete_note, "Foo Bar")

Produces the same error, no such column 'complete_note' in 'tasks' table.

The commit also mentions a change where updated_at doesn't change with update_attribute, does that pose any issues for anyone else or is it just me?

Reported by Hugh Kelsey · July 12th, 2010 @ 04:01 PM

State: wontfix
Milestone: 3.x
Assigned to: Neeraj Singh Neeraj Singh
Importance: Low

Activity

  1. Neeraj Singh
    Neeraj Singh
    • Milestone set to 3.x
    • Tag set to rails 3, activerecord
    • Assigned user set to Neeraj Singh
    • Importance changed from to Low

    July 12th, 2010 @ 05:25 PM

  2. Neeraj Singh
    Neeraj Singh
    • State changed from new to wontfix

    The commit also mentions a change where updated_at doesn't change with update_attribute, does that pose any issues for anyone else or is it just me?

    updated_at/on will always change. I am not able to see where in commit message I mentioned that updated_at/on will not change.

    The idea behind update_attribute is that user should be pretty much assured that update_attribute changes the field in the database. That is why validation and callbacks are not called so that no one can prevent the update to the database.

    case 1

    self.update_attribute(:account, Account.create)
    

    Above case is not supported and that is okay. That case can be changed to

    self.update_attribute(:account_id, Account.create.id)
    

    case 2

    @task.update_attribute(:complete_note, "Foo Bar")
    

    In the above case only database change should take place is to change updated_at/on. And for that you can simply do

    @task.touch
    

    I understand that update_attribute would be used in a lot lesser number of cases and that is the intent. update_attributes should be used in majority of cases and update_attribute should be limited to a few cases where you want to update a field. For example after sending out an email. If a bug prevents marking that field as updated then user might get a number of emails delivered to him/her.

    Discussed the issue with José Valim and he is okay with it.

    July 12th, 2010 @ 09:01 PM