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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:50:32+00:00 2026-06-09T17:50:32+00:00

I have User, which can be one of three types: Admin, Student, Teacher. Everyone

  • 0

I have User, which can be one of three types: Admin, Student, Teacher. Everyone has other attributes. I am trying polymorphic association one-to-one like this:

User

class User < ActiveRecord::Base
    belongs_to :identity, :polymorphic => true
    accepts_nested_attributes_for :identity, :allow_destroy => true

    attr_accessible :email, :login, :remember_token, 
                    :password_confirmation, :password, :role
end

Student

class Student < ActiveRecord::Base
    attr_accessible :field

    has_one :user, :as => :identity
end

Controller

def new
     @user = User.new
end
def create
    @user = User.new(params[:user]) # It fails here.
    @user.identita.build
    ...
end

View

<%= form_for(@user) do |f| %>
    <%= f.label :login %><br />
    <%= f.text_field :login %>

    <%= f.fields_for [:identity, Student.new] do |i| %>  
    <%= i.label :field %><br />
    <%= i.textfield_select :field  %>
    <% end %>
<% end %>

When I submit this view (more complex, but this is the core), it sends hash like this:

{"utf8"=>"✓",
 "authenticity_token"=>"...",
 "user"=>{"login"=>"...",
 "student"=> {"field"=>"..."}
}

So it fails on marked line in controller with:

ActiveModel::MassAssignmentSecurity::Error in UsersController#create
Can't mass-assign protected attributes: student

What am I doing wrong? Something like :as=>”student” or twisting the realationship?

  • 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-09T17:50:34+00:00Added an answer on June 9, 2026 at 5:50 pm

    Firstly, fix:

    <%= f.fields_for [:identity, Student.new] do |i| %>  
    

    to:

    <%= f.fields_for :identity, Student.new do |i| %>
    

    Secondly, you are trying to use accepts_nested_attributes_for on a belongs_to relationship. This is not supported behavior AFAIK. Perhaps try moving that to the Student model:

    class Student < ActiveRecord::Base
      attr_accessible :field
    
      has_one :user, :as => :identity
      accepts_nested_attributes_for :user, :allow_destroy => true
    end
    

    and make the view like this:

    <%= form_for(Student.new) do |i| %>
      <%= i.fields_for :user, @user do |f| %>  
        <%= f.label :login %><br />
        <%= f.text_field :login %>
      <% end %>
      <%= i.label :field %><br />
      <%= i.textfield_select :field  %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application on which a user can create multiple profiles of three
I have a situation in which user can single tap a control, which show
I have make a messaging system in which user can send messages to each
I have the following problem: I have a search box in which user can
I have a text-box into which a user can input a comment. The comment
I have a text editor in which a user can write HTML code. I
I have an NSView in which the user can draw circles. These circles are
I have the following select list from which the user can select multiple values.
I have a controller method called Edit in which the user can edit data
I have two buttons, which the user can click and will open a FileDiagloag

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.