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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:11:16+00:00 2026-05-27T20:11:16+00:00

I am trying to save changes to my database trough a rake task. In

  • 0

I am trying to save changes to my database trough a rake task.

In my rake task I do something like:

namespace :parts do
  desc "Update Parts table, swap names in title"
  task :swap => :environment do
    Part.swap
  end
end

In my Part class I do

def self.swap
  Part.all.each do |part|
    if (part.title =~ REGEX) == 0
      part.title.gsub! REGEX, '\2 \1'
      puts part.title
      part.save!
    end
  end
end

However, this does not save the part. The save! does return true. the puts part.title does return the value I want.

If I call

Part.update(part.id, title: part.title)

The database updates properly. Why is this? Am I doing something wrong in my loop?
I am working with Rails 3.1.3, Rake 0.9.2.2 and MySQL2 0.3.7

  • 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-27T20:11:16+00:00Added an answer on May 27, 2026 at 8:11 pm

    It’s because the way ActiveRecord detects that attributes are changed is through the setter. Therefore, if you use gsub! on an attribute, ActiveRecord doesn’t know it needs to update the database.

    You’ll probably have to do this:

    part.title = part.title.gsub REGEX, '\2 \1'
    

    Update from comment

    Also, if you try to assign title to another variable and then gsub! it won’t work either because it’s the same object (code from my project, variable names different).

    ruby-1.9.3-p0 :020 > t = p.name
     => "test" 
    ruby-1.9.3-p0 :023 > t.object_id
     => 70197586207500 
    ruby-1.9.3-p0 :024 > p.name.object_id
     => 70197586207500 
    ruby-1.9.3-p0 :025 > t.gsub! /test/, 'not a test'
     => "not a test" 
    ruby-1.9.3-p0 :037 > p.name = t
     => "not a test" 
    ruby-1.9.3-p0 :026 > p.save
       (37.9ms)  BEGIN
    ** NO CHANGES HERE **
       (23.9ms)  COMMIT
     => true 
    

    You have to .dup the string before modifying it.

    ruby-1.9.3-p0 :043 > t = p.name.dup
     => "test" 
    ruby-1.9.3-p0 :044 > t.gsub! /test/, 'not a test'
     => "not a test" 
    ruby-1.9.3-p0 :045 > p.name = t
     => "not a test" 
    ruby-1.9.3-p0 :046 > p.save
       (21.5ms)  BEGIN
       (20.8ms)  UPDATE "projects" SET "name" = 'not a test', "updated_at" = '2012-01-02 07:17:22.892032' WHERE "projects"."id" = 108
       (21.5ms)  COMMIT
     => true 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to save data to a database on a button push, but
I am trying to save Arabic language in mysql database but it doesnot save
Im trying to save a bitmap jpg format with a specified encoding quality. However
I'm trying to save some XML-Data in my UserSettings (Properties.Settings.Default.UserSettings) in a .NET Winforms
I'm trying to save a PDF file to SQL Server and I already have
We are trying to save the state of the application on exit and restore
I am trying to save a record which has a many-to-one property mapping. I
I'm trying to save an excel spreadhseet from SQL Server Integration Services 2005. Unfortunatly
Hello I am trying to save a bitmap image in a basic image editor
I am trying to save images using the following code: - (void)writeData{ if(cacheFileName==nil) return;

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.