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

  • Home
  • SEARCH
  • 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 8295423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:34:54+00:00 2026-06-08T14:34:54+00:00

I have a model called DefaultCompany that has no controller, instead I create it

  • 0

I have a model called DefaultCompany that has no controller, instead I create it through the companies_controller which calls the user.set_default_company (defined below) if they check the “default company” checkbox on the form.

Default company is a joining table of user_id and company_id.

class DefaultCompany < ActiveRecord::Base
  attr_accessible :company_id, :user_id
  belongs_to :company
  belongs_to :user
end

I keep getting the following error:

Can't mass-assign protected attributes: company, user 
app/models/user.rb:22:in `set_default_company'
app/controllers/companies_controller.rb:23:in `create'

I’ve set my user model to be able to accept nested attributes for DefaultCompany, like this

class User < ActiveRecord::Base
  has_one :default_company
  accepts_nested_attributes_for :default_company
  attr_accessible :default_company_attributes

  def set_default_company(company)
    exists = DefaultCompany.find(self.id)
    if exists
      exists.update_attributes(company: company)
    else 
      DefaultCompany.create(company: company, user: self)
    end
  end
end

And here is the create action for the companies_controller.rb

def create
@company = Company.new(params[:company])

if @company.save
  if params[:default_company]
    current_user.set_default_company @company.id
  end
  flash[:notice] = "Company was successfully created."
  Role.assign_creator(@company.id, current_user.id)
  redirect_to @company
else
  redirect_to new_company_path
  end
end

So I’m not sure what I need to add so that mass-assignment will pass, can anyone help me figure out / explain this?

  • 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-08T14:34:55+00:00Added an answer on June 8, 2026 at 2:34 pm

    I believe rails is strict about the naming in mass-assignment, so although you’ve whitelisted company_id and user_id, you have not whitelisted company and user.

    Try changing the assignment in set_default_company to:

    if exists
      exists.update_attributes(company_id: company.id)
    else 
      DefaultCompany.create(company_id: company.id, user_id: self.id)
    end
    
    • 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 a voip_phone that has a an attribute schedule which
I have a model called Recipe which has 2 images that use carrierwave, so
So I have a model called Image that belongs_to :user. Each user has a
I have a Model called statistics which has a value field that contains Goals
I have a model called User, and a user has a property relatedUsers, which,
I have a model called Answer which has a ForeignKey relationship to another model
I have a model called Details, and two controller methods new and create. New
I have a model called User, and a controller called ManagersController. Some users are
I have a model Rails model called Orders that has a type_id, location, and
I have a Model called Section which has many articles ( Article ). These

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.