This project is archived and is in readonly mode.

#6024 ✓wontfix
suan

Rails does not save concatataned string attributes

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

>> 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...

Comments and changes to this ticket

  • Ravil Bayramgalin

    Ravil Bayramgalin November 21st, 2010 @ 04:10 AM

    • 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
    
  • Rizwan Reza

    Rizwan Reza November 21st, 2010 @ 01:01 PM

    • State changed from “new” to “wontfix”
    • Importance changed from “” to “Low”
  • Rizwan Reza

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages