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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:22:39+00:00 2026-06-06T15:22:39+00:00

Imagine I have a boolean and a string attribute on my model with data

  • 0

Imagine I have a boolean and a string attribute on my model with data like this:

Model.locked => true
Model.status => "Available"

When the Update action is invoked, I want to use the boolean attribute to control whether the string attribute gets modified by the update, all other attributes in the update should be saved regardless.

So if

Model.locked => true

and we try

Model.status = "Sold"

then

Model.status => "Available"

Currently I have my model like this…

before_update :dont_update_locked_item, :if => :item_locked?

def item_locked?
  self.locked
end

def dont_update_locked_item
  #Some rails magic goes here
end

How do I prevent a single attribute from being updated?

I know the answer is going to hurt, but someone help me out. It’s late, I’m tired and I’m fresh out of talent. 😉

  • 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-06T15:22:41+00:00Added an answer on June 6, 2026 at 3:22 pm

    Hope it doesn’t hurt so much 🙂

    class Item < ActiveRecord::Base
      validate :cannot_update_if_locked, :on => :update
    
      def item_locked?
        self.locked
      end
    
      def cannot_update_if_locked
        errors.add(:item_id, "is locked") if item_locked?
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have a service that looks like this: public interface MyAccountService { boolean
Imagine I have this code: var myFunc1 = function(event) { alert(1); } var myFunc2
Imagine I have a table with two columns, a primary key and some data.
Imagine I have one common functionality: series and number (string and integer) of some
Imagine I have the following string : set(SEXY_STRING I love CMake) then I want
Imagine I have a domain with a Entity and one of properties of this
Imagine I have this simple table: Table Name: Table1 Columns: Col1 NUMBER (Primary Key)
Imagine I have a simple data file called 'test_gnu.txt' as follow: 0.32,0.116,0.398989898989899 0.34,0.010,0.01212121212121212 0.36,0.035,0.11313131313131312
Imagine I have this C function (and the corresponding prototype in a header file)
Imagine you have an application with several hundreds of classes implementing dozens of high

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.