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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:53:51+00:00 2026-05-13T17:53:51+00:00

When I modify an ActiveRecord object during a session, I cannot seem to retrieve

  • 0

When I modify an ActiveRecord object during a session, I cannot seem to retrieve this modified instance in a method call. A simplified example follows:

Assume we have a model with only two objects: Project and Task linked with a 1-n relationship. Both objects can be active, but tasks require their parent project to be active before activating. There are two ways to activate: globally through the Project (which activates all tasks) or individually through a Task.

With the following straightforward implementation, an error occurs:

class Project < ActiveRecord::Base

  # Relations
  has_many :tasks

  def activate
    self.transaction do
      self.active = true
      tasks.each {|task| task.activate}
    end
  end

end

class Task < ActiveRecord::Base

  # Relations
  belongs_to :project

  def activate
    raise ArgumentError, "Cannot activate a task of an inactive project" unless project.active?
    self.active = true
  end

end

Indeed, the console will report

>> project = Project.first
=> #<Project id: 1, name: "Test project", active: false>
>> project.activate
ArgumentError: Cannot activate a task of an inactive project
    from /Rails/cache_issue/app/models/task.rb:7:in `activate'
    from /Rails/cache_issue/app/models/project.rb:9:in `activate'

The problem is that the Project object instance modified in the Project#activate method is not the same that ActiveRecord loads when accessing the Task#project relationship in the Task#activate method. When debugging, both objects are the “same” ActiveRecord record, but not the same Ruby object instance.

>> project = Project.first
=> #<Project id: 1, name: "Test project", active: false>
>> project.activate
"Project#activate:    self.id = 1,    self.object_id = 2176477060"
"   Task#activate: project.id = 1, project.object_id = 2176246440"
ArgumentError: Cannot activate a task of an inactive project
    from /Rails/cache_issue/app/models/task.rb:8:in `activate'
    from /Rails/cache_issue/app/models/project.rb:10:in `activate'

In other ORM systems, fetching a model instance by database identifier always looks in “cache”, at least during a transaction and even during a session. I have tried to eager load the relations, but that does not change the issue since I could still be using another Project instance than the one ActiveRecord decided to link to the Task object.

Is there any technique (or gem or third-party) to get this simple process to work? That is that every reference to the same ActiveRecord record during a session/thread always refers to the same Ruby object instance?

Thanks,

-Jason

  • 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-13T17:53:51+00:00Added an answer on May 13, 2026 at 5:53 pm

    Here are a couple of things you can try.

    1. redefining Project#activate so that it saves the Project before any of the tasks are activated.

      class Project < ActiveRecord::Base    
        # Relations
        has_many :tasks
      
        def activate
          self.transaction do
            save!
            self.active = true
            tasks.each {|task| task.activate}
          end
        end    
      end
      

      Essentially project is being loaded from that database by each as it check the activated status of the associated project. Saving the project first should fix that.

    2. Use autosave and set the tasks active status directly.

      class Project < ActiveRecord::Base    
        # Relations
        has_many :tasks, :autosave => true
      
        def activate
          self.transaction do           
            self.active = true
            tasks.each {|task| task.active = true}
          end
        end    
      end
      

      N.B. Requires Rails 2.3. Also, the tasks of a project will not be activated until the project is saved.

    3. Have Task#activate accept a boolean argument indicating whether or not to check if the associated project is activated. Essentially mirroring ActiveRecord::Base#save.

      class Task < ActiveRecord::Base    
        # Relations
        belongs_to :project
      
        def activate(validate_active_project = true)
          if validate_active_project && ! project.active?
            raise ArgumentError, "Cannot activate a task of an inactive project" 
          end
          self.active = true
        end
      
      end
      
      class Project < ActiveRecord::Base    
        # Relations
        has_many :tasks
      
        def activate
          self.transaction do           
            self.active = true
            tasks.each {|task| task.activate(false)}
          end
        end    
      end
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 317k
  • Answers 317k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See a vaguely similar question I asked with regards to… May 13, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer Note.. Htmlspecialchar only converts some of the characters.. if you… May 13, 2026 at 11:43 pm
  • Editorial Team
    Editorial Team added an answer From the website: // Sometimes, you won't know the row… May 13, 2026 at 11:43 pm

Related Questions

The business logic is this: Users are in a Boat through a join table,
Using SubSonic 3 ActiveRecord, I generated code from an existing database that had foreign
Upon serving a request for an ASPX page, if ASP.NET notices any change to
I am trying to modify an existing WPF application for localization. One of my
I have been asked to modify an Excel sheet with some arcaic programming. I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.