Reputation: 378
rails 3.2.8
I tried to create a simple polymorphic Log model as logable. The table has only one field, log and it was envisioned as just a timestamp and a short message would be appended to a has_one relation.
My problem is that after the record is created, I can't seem to get the message to append and save. Debugging shows it has be committed and the record is changed, but when it is reloaded it reverts to the original version. I've been looking at this way to long and it is time to see if anyone else sees something stupid.
Relevant parts of the models:
class Log < ActiveRecord::Base
belongs_to :logable, :polymorphic => true
attr_accessible :logable_id, :log, :logable_type
def set_log(entry)
self.log << "\r\n#{Time.now.to_s} - #{entry}"
self.save
end
end
class Candidate < ActiveRecord::Base
attr_accessible :citizen_id, :commitment_id, :current_stage, :current_status
has_one :log, :as => :logable
def append_log(entry)
if self.log.nil?
self.build_log( :log => "#{Time.now.to_s} - Log Created")
self.log.save
end
self.log.set_log(entry)
end
end
Below is from the console log where I get a Candidate, look at the log (original created entry only), append a entry, show the changes made in the instance. Reload the log record and the changes are gone.
1.9.2-p136 :001 > c = Candidate.find(1)
Candidate Load (15.1ms) SELECT "candidates".* FROM "candidates" WHERE "candidates"."id" = ? LIMIT 1 [["id", 1]]
1.9.2-p136 :002 > c.log
Log Load (0.1ms) SELECT "logs".* FROM "logs" WHERE "logs"."logable_id" = 1 AND "logs"."logable_type" = 'Candidate' LIMIT 1
=> #<Log id: 1, logable_id: 1, logable_type: "Candidate", log: "2012-11-16 15:23:49 -0600 - Log Created", created_at: "2012-11-16 21:23:49", updated_at: "2012-11-16 21:23:49">
1.9.2-p136 :003 > c.append_log("Add entry")
LOG BEFORE 2012-11-16 15:23:49 -0600 - Log Created
(0.1ms) begin transaction
(0.1ms) commit transaction
LOG APPENDED 2012-11-16 15:23:49 -0600 - Log Created
2012-11-16 16:05:53 -0600 - Add entry
NOTE Log field has been appended
=> nil
1.9.2-p136 :004 > c.log
=> #<Log id: 1, logable_id: 1, logable_type: "Candidate", log: "2012-11-16 15:23:49 -0600 - Log Created\r\n2012-11-16...", created_at: "2012-11-16 21:23:49", updated_at: "2012-11-16 21:23:49">
NOTE Now reload the log record
1.9.2-p136 :005 > l = Log.find(1)
Log Load (0.3ms) SELECT "logs".* FROM "logs" WHERE "logs"."id" = ? LIMIT 1 [["id", 1]]
=> #<Log id: 1, logable_id: 1, logable_type: "Candidate", log: "2012-11-16 15:23:49 -0600 - Log Created", created_at: "2012-11-16 21:23:49", updated_at: "2012-11-16 21:23:49">
NOTE The add entry line is gone!
I don't know if this is a problem with the has_one relation, but even if I just call set_log with just the log record, at appears to append, but reloading does not have the changes.
Stumped Steve
Upvotes: 2
Views: 3006
Reputation: 19899
Are you showing all of the rails log? After you call c.append_log
I would expect to see an UPDATE
SQL log entry, but there isn't one. That suggests that self.save
in set_log
is failing. Spit out the .errors
on c.log
and see if a validation is failing...
Turns out it's because <<
doesn't twiddle the dirty attribute bits.
Changing <<
to +=
should do the trick.
self.log += "\r\n#{Time.now.to_s} - #{entry}"
Upvotes: 1
Reputation: 2124
try to
self.save!
which should raise the error(s) if there are any
you can also raise your own exception or do magic like
if !self.save
raise "attempt to save failed"
end
Upvotes: 2