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 6803799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:23:18+00:00 2026-05-26T19:23:18+00:00

I want to swap the content in answers table with ActiveRecord. code 1: Archieve::Answer.find_each

  • 0

I want to swap the content in answers table with ActiveRecord.
code 1:

Archieve::Answer.find_each do |answer|
  str = answer.content
  dosomething() #change the value
  answer.update_attribute(:content,str)
end

But It doesn’t change the value of content.

code 2:

Archieve::Answer.find_each do |answer|
  str = answer.content
  dosomething() #change the value
  answer.reload
  answer.update_attributes(
    :content => str
  )
end

Before update the :content attributes, I reload the record every time.
It can indeed change the the value.
Why?
What’s the difference between code 1 & code 2?
Source Code

###1 Post Debug Message:
Updated Post:

Changed?: false 
valid?: true 
errors: #<ActiveModel::Errors:0xa687568> 
errors: #<ActiveModel::Errors:0xa687568 @base=#<Archieve::Answer id: 9997190932758339, user_id: 4163690810052834, question_id: 3393286738785869, content: "狗狗生病,好可怜呀,", is_correct: false, votes_count: 0, comments_count: 0, created_at: "2011-11-06 18:38:53", updated_at: "2011-11-06 18:38:53">, @messages={}>
  • 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-05-26T19:23:19+00:00Added an answer on May 26, 2026 at 7:23 pm

    possible ActiveRecord 3.1.1 bug

    The OP mentioned to me that he uses require "active_record" in a stand alone script (not using rails runner).
    There is no separate Rails application for his task, he just uses a script. This is not necessarily bad, and has worked in earlier ActiveRecord versions, e.g. 2.x AFAIK — maybe this is a regression in Rails 3.1 due to a new dependency?

    # the OP's require statements:
    require 'rubygems'
    require 'logger'
    require 'yaml'
    require 'uuidtools'
    require 'active_record'
    

    complete code here: https://raw.github.com/Zhengquan/Swap_Chars/master/lib/orm.rb

    maybe a dependency is missing, or problem with AR 3.1.1 when initialized stand alone?

    It could be a bug actually

    It could be that update_attribute() triggers a bug in the dirty-tracking of attributes, which then incorrectly assumes that the object has not changed, and as a result it will not be persisted, although the implementation of update_attribute() calls save() (see code fragment below).

    I’ve seen something like this with an older version of Mongoid — could be that there is a similar hidden bug in your ActiveRecord version for update_attribute()

    In the Rails Console monkey-patch update_attribute like this:

    class ActiveRecord::Base
      def update_attribute(name, value) # make sure you use the exact code of your Rails Version here
        send(name.to_s + '=', value)
        puts "Changed?: #{changed?}"        # this produced false in the OP's scenario
        puts "valid?: #{valid?}"
        puts "errors: #{errors.inspect}"
        save
      end
    end
    

    then try to run your Code 1 again…

    you shouldn’t see “Changed?: false”.. if it returns false, although you changed the attribute, then there is a bug in your ActiveRecord version and you should report it.

    Code 1:

    NOTE: check the definition of update_attribute() (singular) here:
    (please read the fine-print regarding validations — it doesn’t sound like a good idea to use that method)

    http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000400

    See also:

    Rails: update_attribute vs update_attributes

    The source code for update_attribute() looks like this:

    2260:       def update_attribute(name, value)
    2261:         send(name.to_s + '=', value)
    2262:         save
    2263:       end
    

    it could fail if there is a bug with the dirty-tracking of attributes…

    Code 2:

    The second code looks correct.

    There are a couple of things to also consider:

    1) which attributes did you define as accessible, via attr_accessible ?

    e.g. only accessible attributes will be updated via update_attributes()

    http://apidock.com/rails/ActiveRecord/Base/update_attributes

    2) which validations do you use?

    are you sure the validations pass for the record when you call update_attribute?

    See also:

    http://guides.rubyonrails.org/active_record_querying.html

    http://m.onkey.org/active-record-query-interface

    http://api.rubyonrails.org/classes/ActiveRecord/Base.html

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

Sidebar

Related Questions

Suppose we have the following code. I want to swap out the inner parse_place
I want to swap two integers, and I want to know which of these
want to know why String behaves like value type while using ==. String s1
Want the function to sort the table by HP but if duplicate HPs then
Want to code a key pad for an calculator. What I want to make
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
I want to swap images on the button dynamically using mvvm light. When the
I have a HTML table with following content. <table class='main'> <tr id='r1' class='tbl'> <td>V1</td>
I want to swap out one view with another by pushing the old view
I want to swap to tables in the best possible manner. I have an

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.