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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2012-Nov-17 09:27 UTC
Re: Problem with record not being saved but no errors
On Nov 16, 12:46 pm, AppleII717 <sa...-ee4meeAH724@public.gmane.org> wrote:> 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 >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> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.