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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:37:41+00:00 2026-06-11T13:37:41+00:00

I’m doing my first steps in Rails and in object-oriented programming. There is something

  • 0

I’m doing my first steps in Rails and in object-oriented programming.
There is something quite fudemental that I would like to understand: why do we need attr_accessible within the model?

I have read that hackers can use mass-assignment in order to change database entries and therefore compremise security, and that’s why sensitive fields need protection (using atribute_protected in this case).

Is attr_accessible the opposite of atribute_protected? If so, why do we need to state which fields are accessible and which are not? aren’t those fields accessible by defult? And what is attr_accessor used for?

I noticed that if I don’t make some fields acessible, my application doesn’t run. Can I use attr_acessible for sensitive fields like :password_digest and :admin?

It would be amazing if someone could explain it to me.

All the best,
TimmyOnRails

  • 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-11T13:37:42+00:00Added an answer on June 11, 2026 at 1:37 pm

    You’ve got a couple of concepts mixed together here, so I’ll try to untangle them.

    attr_accessor is for setting up a readable and writable attribute. It is the equivalent of saying attr_reader and attr_writer. Since your question isn’t directly about attr_accessor, I won’t address it anymore than saying check out this link on Accessors.


    According to the Rails docs: attr_accessible is the opposite of the attr_protected macro

    You’re correct that these methods are used to prevent Mass Assignment vulnerabilities.

    • attr_accessible says which attributes can be set by mass assignment.
    • attr_protected says which attributes cannot be set by mass assignment.

    So what’s the use case for each? In one case you’re able to set a global config option that makes it so that all attributes must be declared attr_accessible:

    config.active_record.whitelist_attributes = true
    

    In that case you’d use attr_accessible frequently.

    And attr_protected? If you went the opposite way and said false on whitelisting attributes, how would you declare which attributes shouldn’t be mass assignable? If you said attr_protected you’re right! 😀

    Typically you’d want to set fields like :admin as attr_protected because you don’t want an attacker coming in and escalating their privileges to an admin role.

    Mass assignment is not something that’s easy to get right. Big, smart development teams have gotten this wrong. So tred carefully and make sure you understand what’s going on!

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

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.