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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:35:29+00:00 2026-05-26T04:35:29+00:00

I have a model with two attr_accessible lines attr_accessible …., :as => :user attr_accessible

  • 0

I have a model with two attr_accessible lines

attr_accessible ...., :as => :user
attr_accessible ...., :as => :admin 

Then in my controller I do this

if @user.update_attributes(params[:user],:as => :user)

And I get wrong number of parameters 2 for 1, however: @user.assign_atrributes(params[:user],:as => :user) works.

I am using mongoid. Any ideas?

  • 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-26T04:35:29+00:00Added an answer on May 26, 2026 at 4:35 am

    I think you found something that has not been implemented yet in your version of Mongoid — but please check a later Mongoid version, where it seems to be implemented since 2.2.1!

    The doc says that :as is a valid option in Mongoid::Document

    http://mongoid.org/docs/documents/access.html (see bottom of page)

    But it says: You can scope the mass assignment by role by providing the role as an option to the constructor or create methods.

    It doesn’t speak of update_attributes in particular – update_attributes is not a constructor

    Checking in the source code reveals that it’s not implemented in update_attributes() in Mongoid versions < 2.2.1 , but later versions have it implemented!

    If you’re using a later version of Mongoid, and still experience problems,
    I would recommend to post this on the Google Mongoid Group as a bug, mentioning your Mongoid version number.

    See also:

    http://groups.google.com/group/mongoid/


    EDIT:

    Looks like it’s a missing feature in Mongoid 2.1.7

    in the mongoid source code, update_attributes() calles write_attributes(), which does not know about the option :as

    but if you look at the source code of Mongoid 2.3.1 , you’ll see that it’s implemented there since 2.2.1!

    write_attributes() calls assign_attributes , which honors the option :as

    # Allows you to set all the attributes for a particular mass-assignment security role                                                                                                                                                                                                                                   
    # by passing in a hash of attributes with keys matching the attribute names                                                                                                                                                                                                                                             
    # (which again matches the column names)  and the role name using the :as option.                                                                                                                                                                                                                                       
    # To bypass mass-assignment security you can use the :without_protection => true option.                                                                                                                                                                                                                                
    #                                                                                                                                                                                                                                                                                                                       
    # @example Assign the attributes.                                                                                                                                                                                                                                                                                       
    #   person.assign_attributes(:title => "Mr.")                                                                                                                                                                                                                                                                           
    #                                                                                                                                                                                                                                                                                                                       
    # @example Assign the attributes (with a role).                                                                                                                                                                                                                                                                         
    #   person.assign_attributes({ :title => "Mr." }, :as => :admin)                                                                                                                                                                                                                                                        
    #                                                                                                                                                                                                                                                                                                                       
    # @param [ Hash ] attrs The new attributes to set.                                                                                                                                                                                                                                                                      
    # @param [ Hash ] options Supported options: :without_protection, :as                                                                                                                                                                                                                                                   
    #                                                                                                                                                                                                                                                                                                                       
    # @since 2.2.1                                                                                                                                                                                                                                                                                                          
    def assign_attributes(attrs = nil, options = {})
      _assigning do
        process(attrs, options[:as] || :default, !options[:without_protection]) do |document|
          document.identify if new? && id.blank?
        end
      end
    end
    

    You can find the source code this way:

    $ find   ~/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.1.7/lib/ -type f -exec grep -l 'def write_attributes' {} \;
    ~/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.1.7/lib/mongoid/attributes.rb
    $ emacs ~/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.1.7/lib/mongoid/attributes.rb
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Details, and two controller methods new and create. New
I have a hibernate model with two classes, let's say A and B. B
I have a simple data model of two tables, email and recipients, email can
I have a Django model TimeThingie with two TimeField s called t1 and t2
In my Core Data model I have two entities: List and Patient. List has
Given two model classes, Foo and Bar , I want Foo to have 3
I have two tables: Client(id,name,...) Purchase(id,item,date,client_id,...) They have their respective Model, with their validations.
I have an Auction model with several attributes, two of which are current_auction:boolean and
I have two models: The model NetworkObject try to describe hosts. I want to
I'm having a difficult time understanding how to do this. I have two models,

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.