Forum: Ruby on Rails Problem with record not being saved but no errors

Af48b3dddc0617ae1feca1d5488f0e42?d=identicon&s=25 Steve Alex (salex)
on 2012-11-17 10:13
(Received via mailing list)
I tried to implement a simple model_logger table that is polymorphic
as logable. Wanted nothing more than a text field (log) that text is
appended to.

The ModelLog model just has a simple set_log method
  def set_log(entry)
    self.log << "\r\n#{Time.now.to_s} - #{entry}"
    self.save
    #puts "Model Log Saved  #{errors.inspect}"
  end

I also have a set_log in the logable model that creates the
polymorphic has_one if not there then calls the set_log in ModelLog.
Nothing difficult and it all appears to work in the console - commit
messages etc, but the above save does not appear to be committed. If I
add messages/lines to the record the appear until I get a fresh copy
of the record and it only contains the original created entry

I've spent about 6 hours looking at this I am stumped. I added a log
message to a show page and it has the same behavior. A message is
appended, but each call only shows the original log created message
and the last appended.

Below is console log that fetches a record, adds a message, displays
the changes, reloads the record and it is gone:

1.9.2-p136 :011 > ml = ModelLog.find(10)
  ModelLog Load (0.2ms)  SELECT "model_logs".* FROM "model_logs" WHERE
"model_logs"."id" = ? LIMIT 1  [["id", 10]]
 => #<ModelLog id: 10, logable_id: 3, logable_type: "Candidate", log:
"2012-11-16 05:26:52 -0600 - Created", created_at: "2012-11-16
11:26:52", updated_at: "2012-11-16 11:26:52">
##NOTICE ONLY CREATE ENTRY IN log
1.9.2-p136 :012 > ml.set_log("just set and save")
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction
Model Log Saved  #<ActiveModel::Errors:0x000001020e3500
@base=#<ModelLog id: 10, logable_id: 3, logable_type: "Candidate",
log: "2012-11-16 05:26:52 -0600 - Created\n2012-11-16 05:5...",
created_at: "2012-11-16 11:26:52", updated_at: "2012-11-16 11:26:52">,
@messages={}>
 => nil
1.9.2-p136 :013 > ml
 => #<ModelLog id: 10, logable_id: 3, logable_type: "Candidate", log:
"2012-11-16 05:26:52 -0600 - Created\n2012-11-16 05:5...", created_at:
"2012-11-16 11:26:52", updated_at: "2012-11-16 11:26:52">
##NOTICE CREATE ENTRY AND APPENDED ENTRY in log

1.9.2-p136 :014 > ml = ModelLog.find(10)
  ModelLog Load (0.2ms)  SELECT "model_logs".* FROM "model_logs" WHERE
"model_logs"."id" = ? LIMIT 1  [["id", 10]]
 => #<ModelLog id: 10, logable_id: 3, logable_type: "Candidate", log:
"2012-11-16 05:26:52 -0600 - Created", created_at: "2012-11-16
11:26:52", updated_at: "2012-11-16 11:26:52">
##APPENDED MESSAGE GONE
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2012-11-17 10:28
(Received via mailing list)
On Nov 16, 12:46pm, AppleII717 <sa...@mac.com> wrote:
>
Rails only saves attributes that have changed, and in place
modifications such as << or the destructive string operations (those
that end with !) fool the change tracking mechanism.
Either use += instead of << or call log_will_change! to tell it that
you have changed that attribute.

Fred
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.