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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:52:51+00:00 2026-05-27T10:52:51+00:00

In Rails guide in http://edgeguides.rubyonrails.org/security.html , section 6.1, it introduces a role for attr_accessible

  • 0

In Rails guide in http://edgeguides.rubyonrails.org/security.html, section 6.1, it introduces a role for attr_accessible with :as option,

attr_accessible :name, :is_admin, :as => :admin

My question is, if a user log in, where and how can I assign the user to :admin role so she/he gets the right to mass assign with attr_accessible? Also can I define my own role such as group_to_update? If it does, what should go into the definition of group_to_update?

Thanks.

  • 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-27T10:52:52+00:00Added an answer on May 27, 2026 at 10:52 am

    You are using some technical terminology in vague ways that is making your understanding of this process muddled, so I’m going to clear up this terminology first.

    where and how can I assign the user to :admin role

    The ‘role’ used in the :as parameter to attr_accessible is not a user role. It is an attribute role. It means that attribute is protected from overwriting unless that role is specified in the statement that sets the attribute. So, this system is independent of any user system. Your application doesn’t even need to have users to have roles in mass assignment.

    can I define my own role such as group_to_update

    Roles are not really “defined” in any formal sense at all. In any place that a role is expected, simply use any symbol/string (e.g. :group_to_update) as the role. No need to specify it anywhere else ahead of time.

    Here’s how it works. Normally, during mass assignment of a hash to model attributes, all of the model’s attributes are used as keys to the assigned hash. So if you have a Barn model and barn instance of it, with three attributes horse, cat, and rabbit, then this:

    barn.attributes = params
    

    Is essentially the same as doing:

    barn.horse = params[:horse]
    barn.cat = params[:cat]
    barn.rabbit = params[:rabbit]
    

    Now, if you set any attr_accessible on the barn model, only the attributes you set there will be updated when you use mass assignment. Example:

    class Barn < ActiveRecord::Base
        attr_accessible :cat
        attr_accessible :rabbit
    end
    

    Then this:

    barn.attributes = params
    

    Will only do this:

    barn.cat = params[:cat]
    barn.rabbit = params[:rabbit]
    

    Because only ‘cat’ and ‘rabbit’ are set to accessible (‘horse’ is not). Now consider setting an attribute role like this:

    class Barn < ActiveRecord::Base
        attr_accessible :cat
        attr_accessible :rabbit, :as => :banana
    end
    

    First, note that the the role can by anything you want as long as it is a symbol/string. In this case, I made the role :banana. Now, when you set a role on an attr_accessible attribute, it normally does not got assigned. This:

    barn.attributes = params
    

    Will now only do this:

    barn.cat = params[:cat]
    

    But you can assign attributes using a specific role by using the assign_attributes method. So you can do:

    barn.assign_attributes(params, :as => :banana)
    

    This will assign all normally-protected params as well as all params protected under the role :banana:

    barn.cat = params[:cat]
    barn.rabbit = params[:rabbit]
    

    So consider a longer example with more attributes:

    class Barn < ActiveRecord::Base
        attr_accessible :cat
        attr_accessible :rabbit, :as => :banana
        attr_accessible :horse, :as => :banana
        attr_accessible :cow, :as => :happiness
    end
    

    Then you can use those roles when assigning attributes. This:

    barn.assign_attributes(params, :as => :banana)
    

    Corresponds to:

    barn.cat = params[:cat]
    barn.rabbit = params[:rabbit]
    barn.horse = params[:horse]
    

    And this:

    barn.assign_attributes(params, :as => :happiness)
    

    Corresponds to:

    barn.cat = params[:cat]
    barn.cow = params[:cow]
    

    Now, if you choose to, you can make user roles (e.g. a “role” column on your User model) correspond to attribute roles on any model. So you could do something like this:

    barn.assign_attributes(params, :as => user.role)
    

    If this user‘s role happens to be banana, then (using our last model example) it will set attributes on barn for cat, rabbit, and horse. But this is just one way to use attribute roles. It is entirely up to you if you want to use them a different way.

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

Sidebar

Related Questions

Cookies maximum size in rails application,as per rails guide it is 4 KB. http://guides.rubyonrails.org/security.html#session-storage
Unfortunately official guide for creating Rails plugins is outdated: http://guides.rubyonrails.org/plugins.html and I didn't find
I am following the guide http://edgeguides.rubyonrails.org/plugins.html and it seems to be slightly outdated. Could
Let's say I have a nested resource like in the Rails guide example: http://guides.rubyonrails.org/routing.html#nested-resources
The rails docs are apparently in error - http://guides.rubyonrails.org/association_basics.html#selfjoins ) In designing a data
I am going working through the Rails Guides (http://guides.rubyonrails.org/getting_started.html),and am stuck at item 11
I am following the guide from http://guides.rubyonrails.org/i18n.html to add several translation to my page,
I have read the following rails guide : http://guides.rails.info/active_record_querying.html in there exemple a client
Im using the following guide for getting started with rails for ubuntu 9.10. http://guides.rails.info/getting_started.html
I am learning ruby from this guide http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec:deploying , and I am trying to

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.