Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:21:02+00:00 2026-06-14T11:21:02+00:00

rails 3.2.8 I tried to create a simple polymorphic Log model as logable. The

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:21:03+00:00Added an answer on June 14, 2026 at 11:21 am

    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}"

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started with Ruby On Rails, and want to create a simple web
I've created a simple rails app that has a people and a notes model.
Just tried writing a simple validates_presence_of in my model, and when the errors try
I'm trying to create a migration for a simple table that is just used
I have a simple user registration page in my rails app that has two
In my first rails project (using mysql) I tried to execute the rake db:create
I'm new to Heroku. I tried pushing a simple test Rails 3.1.1 app to
Today I tried to follow the basic Twitter tutorial on : --> http://www.noupe.com/ajax/create-a-simple-twitter-app.html But
Trying to create a simple flickr application using Ruby on Rails 3.2.3, I faced
I've been following the Ruby On Rails Tutorial and tried to get rid of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.