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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:46:25+00:00 2026-06-03T21:46:25+00:00

I am using Ruby on Rails 3.2.2 and I would like to run some

  • 0

I am using Ruby on Rails 3.2.2 and I would like to run some check based on the previous status of an @article instance before that it is stored in the database. That is, I have to run some method that make use of data already stored in the database related to @article (in the below case that data is {:id => 1, :title => "Sample title", :content => "Sample content", :status => "private"} and not {:id => 1, :title => "Sample title changed!", :content => "Sample content", :status => "private"}).

I thought to proceed “retrieving”/”re-building” the original instance after that its attribute values have changed. That is:

# In the controller file

# @article.attributes
# => {:id => 1, :title => "Sample title", :content => "Sample content", :status => "private"}
@article.update_attributes(params[:article])
# => {:id => 1, :title => "Sample title changed!", :content => "Sample content", :status => "private"}


# In the model file
before_save :retrieve_original

def retrieve_original
  # self.attributes
  # => {:id => 1, :title => "Sample title changed!", :content => "Sample content", :status => "private"}

  original = ... # ?
  # original.id
  # => nil
  # original.attributes
  # => {:title => "Sample title", :content => "Sample content", :status => "private"}
  # self.attributes
  # => {:id => 1, :title => "Sample title changed!", :content => "Sample content", :status => "private"}
end

So, original should be an Article object and just a duplicate of the @article instance (without the id and without that @article attributes have changed).

I tried to play with some Ruby on Rails method (like ActiveRecord::Base.dup or ActiveModel::Dirty.changed_attributes), but I have not found a solution. Furthermore, I think that there’s a better way to accomplish what I would like to make compared to my approach. If so, what I might do?


I would like to do that because, for example, if an article is public it should be not possible to change the title value; if the article is private, it should be possible to change the title value. Of course, I have to make that check before to store the edited article data and for all attempts that try to update that data (for this I chose to use a before_save callback). However, if the user try to change the title value and the private value to public, then all checks run in a public “context”, but I would like that those checks still run in a private “context” since the previous status was private.

  • 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-03T21:46:27+00:00Added an answer on June 3, 2026 at 9:46 pm

    It looks like you can go through the changes hash in your hook. I.e. dup or clone the changed version, then go through changed version’s changes and set the attributes back to what they were for your copy.

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

Sidebar

Related Questions

I am using Ruby on Rails 3.0.7 and I would like to find some
I am using Ruby on Rails 3 and I would like to know some
I am using Ruby on Rails 3.2.2 and in my controllers I would like
I am using Ruby on Rails 3.2.2 and rspec-rails-2.8.1. I would like to output
I'm using Authlogic with my Ruby on Rails App. I would like user to
I am using Ruby on Rails 3.1 and I would like to know (for
I am using Ruby on Rails 3.0.9 and I would like to seed the
I am using Ruby on Rails 3.1 and I would like to understand how
I am using Ruby on Rails 3.0.7 and I would like to document my
I would like to write a Ruby on Rails application that consumes a RESTful

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.