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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:56:27+00:00 2026-06-05T08:56:27+00:00

I have a user model: class User < ActiveRecord::Base attr_accessible :email, :name # admin

  • 0

I have a user model:

class User < ActiveRecord::Base
  attr_accessible :email, :name
  # admin only
  attr_accessible :email, :name, :admin, :as => :admin

And the following model spec:

describe "accessible attributes" do

  let(:new_user){ FactoryGirl.create(:user) }
  @admin_attrs = { admin: true, name: "ben", email: "xyz@test.com"}

  it "can not be set on create" do

    # variant 1 - test fails, AM::MAS::Error NOT thrown
    expect do
      User.new(@admin_attrs)
    end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)

    # variant 2 - test passes, AM::MAS::Error NOT thrown
    expect do
      User.new(admin: true, name: "ben", email: "xyz@test.com", password: "123xyz", password_confirmation: "123xyz")
    end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)

  end

I can’t figure out why variant 1 of my test fails, but variants 2 passes. The error message I get is:

1) User accessible attributes can not be set on create
     Failure/Error: expect do
       expected ActiveModel::MassAssignmentSecurity::Error but nothing was raised

They are basically the same test. What am I doing wrong? In fact, if I perform test 1 from the console, it does throw a MAS::Error, as expected. I’m confused.

  • 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-05T08:56:29+00:00Added an answer on June 5, 2026 at 8:56 am

    @admin_attrs is probably nil as far as your first text example is concerned because it’s not being set up in a before hook (or through ‘let’)

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

Sidebar

Related Questions

I have a model as follows: class User < ActiveRecord::Base attr_accessible :name, :email, :twitter,
I have this code in my user model: class User < ActiveRecord::Base attr_accessible :email,
Lets say we have some basic AR model. class User < ActiveRecord::Base attr_accessible :firstname,
I have two models: User (email:string) Profile (name:string) class User < ActiveRecord::Base has_one :profile
I have the following models: class EventParticipant < ActiveRecord::Base belongs_to :event has_one :user attr_accessible
I have the following code for my user model: class User < ActiveRecord::Base has_secure_password
I have the following model: class User: name = models.CharField( max_length = 200 )
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to
I have a model class as shown below: class Product < ActiveRecord::Base belongs_to :user;

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.