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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:25:07+00:00 2026-06-14T15:25:07+00:00

In a Rails app, is it possible to make a batch of changes all

  • 0

In a Rails app, is it possible to make a batch of changes all at once, validating only the end result of the changes rather than each individual step?

That was a slightly nebulous question, so let’s make it concrete. Suppose I have a grocer’s store app:

class Store < ActiveRecord::Base
  has_many :fruits
end

class Fruit < ActiveRecord::Base
  belongs_to :store
  attr_accessible :kind, :number, :price
  validates_uniqueness_of :kind, :scope => :store_id
end

I’ve seeded the database with the following data:

fruits:
 store_id |    kind    | number | price
----------------------------------------
        1 | apple      |     10 | 0.15
        1 | banana     |     80 | 0.02
        1 | cherry     |     25 | 0.50

But I realise I’ve messed up, and swapped the number and price of bananas and cherries. I can’t do this:

store = Store.find(1)
old_bananas = store.fruits.where("kind = banana").first
old_cherries = store.fruits.where("kind = cherry").first
old_bananas.kind = "cherry"
old_cherries.kind = "banana"
old_bananas.save! # => Validation Error
old_cherries.save!

because the uniqueness constraint is violated when saving the first row, even though it’d be fine after saving the second. Is it possible to batch both saves together, and have the validation see only the result of making all the changes at once?

(I can think of workarounds – for instance, if I made the validation :allow_nil, then nil off each row before changing any of them – but I’d like to know if a “proper” solution exists)

  • 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-14T15:25:09+00:00Added an answer on June 14, 2026 at 3:25 pm

    If your constraint is only the validation, but you have no unique index enforcing that in the database, use the update_attribute method – it skips validations.

    store.fruits.where("kind = banana").first.update_attribute(:kind, "cherry")
    store.fruits.where("kind = cherry").first.update_attribute(:kind, "banana")
    

    Or simply use save(validate: false) instead of save!.

    EDIT: Since you have a unique index, the dead simple solution is to use a temporary value:

    store.fruits.where("kind = banana").first.update_attribute(:kind, "foo")
    store.fruits.where("kind = cherry").first.update_attribute(:kind, "banana")
    store.fruits.where("kind = foo"   ).first.update_attribute(:kind, "cherry")
    

    I’m not aware of a way to swap values between rows in-place in SQL, which is really the problem here as the model-level validations can easily be bypassed.

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

Sidebar

Related Questions

I'm testing a Rails 3.2.6 app. Is it possible to make an Rspec/Capybara assertion
In a Rails app, I'd like to make a HTTP HEAD request for a
I was hoping to make a Rails app usable both as an Engine and
I'm creating a Rails 3.2 app at the moment and I'm trying to make
Possible Duplicate: Perplexing Ruby/MySQL Error: "invalid packet: sequence number mismatch" My rails app just
To make a long explanation short, suffice it to say that my Rails app
I am busy making a ruby on rails app. I want to make an
I need to make a backup system for my rails app but this has
My rails app is set up using a mysql database. When I do a
Moving rails app to production and running into the routing issues. I've read this

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.