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.

Rails does not save concatataned string attributes

#6024
>> r = Role.first
=> #<Role role_id: 1, name: "bla", description: "bla">
>> str = (r.description << "oink")
=> "blaoink"
>> r.description = str
=> "blaoink"
>> r.save
=> true
>> r.reload
=> #<Role role_id: 1, name: "bla", description: "bla">

Notice how the "description" field doesn't change.
HOWEVER, this works:

>> r = Role.first
=> #<Role role_id: 1, name: "bla", description: "bla">
>> r.description = "#{r.description}oink"
=> "blaoink"
>> r.save
=> true
>> r.reload
=> #<Role role_id: 1, name: "bla", description: "blaoink">

this is just a simple activerecord object and "description" is just a simple text field. Rails 2.3.8. VERY weird...

Reported by suan · November 21st, 2010 @ 01:48 AM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Ravil Bayramgalin
    Ravil Bayramgalin
    • Tag set to invalid

    ActiveRecord uses partial updates. It means that only changed (dirty) attributes will be updated. To keep track of dirty attributes ActiveRecord checks on assignment if new value is equal to old one if not attribute will be marked as dirty.

    In your example,

    str = (r.description << "oink")
    

    you changed description attribute in place without assignment, so later on when you do make an assignment

    r.description = str
    

    description already equals str, so it's not marked as changed hence this attribute is not updated on save.

    If you really need to use in place operation then you should mention it beforehand

    r.description_will_change!
    r.description << 'oink'
    r.save # works as intended
    

    November 21st, 2010 @ 04:10 AM

  2. Rizwan Reza
    Rizwan Reza
    • State changed from new to wontfix
    • Importance changed from to Low

    November 21st, 2010 @ 01:01 PM

  3. Rizwan Reza