Skip to content

unsaved changes via gsub! #4954

Closed
releu opened this Issue Feb 9, 2012 · 2 comments

3 participants

@releu
releu commented Feb 9, 2012

Hi!

I'm trying to correct a comment of the message, but record does not update in the db.

c = Comment.last
# => #<Comment id: 15, question: false, content: "it is inpossible :( ", user_id: 2, post_id: 16, likes_count: 0, created_at: "2012-02-09 04:33:50", updated_at: "2012-02-09 04:33:50">
c.content.gsub! /inp/, 'imp'
# => "it is impossible :( "
c.content
# => "it is impossible :( "
c.save
# (2.2ms)  BEGIN
# User Load (1.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
# (1.1ms)  COMMIT
# true
c.content_was
# => "it is impossible :( "
c.content_changed?
# => false
@lest
lest commented Feb 9, 2012

Confirmed on master. Here is a test https://gist.github.com/6101aae3cb48b31e6c4a.
I'm going to investigate further.

@fxn
Ruby on Rails member
fxn commented Feb 9, 2012

ActiveModel::Dirty does not track in-place edits.

You need either to assign the result of a non-bang gsub to the attribute (that is, work with the attribute writers), or else call c.content_will_change!.

I am closing this issue because this is the documented behavior, and also was discussed at length in #2655.

@fxn fxn closed this Feb 9, 2012
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.