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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:12:44+00:00 2026-05-14T07:12:44+00:00

What is the difference between attr_accessible(*attributes) & attr_protected(*attributes) ? Examples would be nice. I

  • 0

What is the difference between attr_accessible(*attributes) & attr_protected(*attributes)? Examples would be nice.

I see many developers use these in their models. I googled for the differences, but I don’t get exactly what they are. What is the importance and its necessity in different scenarios?

  • 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-14T07:12:44+00:00Added an answer on May 14, 2026 at 7:12 am

    attr_accessible (documentation) says “the specified attributes are accessible and all others are protected” (think of it as whitelisting.)

    whereas

    attr_protected (documentation) says “the specified attributes are protected and all others are accessible” (think of it as blacklisting.)

    A protected attribute is one that can only be modified explicitly (e.g. via attribute=) and can’t be updated via mass assignment (e.g. using model.update_attributes or by passing attributes to new). The behaviour upon an attempt to update a protected attribute via mass assignment depends on the mass_assignment_sanitizer setting (see the update below).

    The classic example would be if a User model had an is_admin attribute you could protect that attribute to prevent form submissions that would allow any user to be set as an administrator.

    example:

    class User < ActiveRecord::Base
      # explicitly protect is_admin, any new attributes added to the model
      # in future will be unprotected so we need to remember to come back
      # and add any other sensitive attributes here in the future
      attr_protected :is_admin
    end
    

    compared with:

    class User < ActiveRecord::Base
      # explicitly unprotect name and bio, any new attributes added to the model
      # in the future will need to be listed here if we want them to be accessible
      attr_accessible :name, :bio
    end
    

    Now, assuming is_admin attribute is protected:

    > u = User.find_by_name('mikej')
    > u.is_admin?
    false
    > u.update_attributes(:name => 'new name', :is_admin => true)
    > u.is_admin?
    false
    > u.name
    "new name" 
    > u.is_admin = true # setting it explicitly
    > u.save
    > u.is_admin?
    true
    

    Update: Later versions of Rails introduced the concept of a mass assignment sanitizer to control the behaviour upon attempts to update protected attributes via mass assignment. In Rails 3.2 and later this can be controlled by setting mass_assignment_sanitizer in config. The default is to just log the attempts and allow code execution to continue, but the standard environment config for development sets this to :strict which raises as exception on an attempt to update a protected attribute.

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

Sidebar

Related Questions

JAVA : is there a difference between the two references p && pp? PrintStream
In Rails, what is the difference between attr_accessor and attr_accessible ? From my understanding,
i don't really understand the difference between the ? and @ in those examples
I would like to know the difference between saving data to an element using
Difference between list & arraylist in android. How can i sort out arraylist?
I cannot find the difference between these two selectors. Both seem to do the
I've been writing Rails for a few years, and the difference between attr_accessible :person
Difference between start-pointers and interior-pointers and in what situation we should prefer one over
The difference between Chr and Char when used in converting types is that one
What's the difference between CopyIcon and DuplicateIcon ?

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.