The following is my model structure
role.rb
has_many :user_roles
has_many :users, through: :user_roles
has_many :companies, through: :user_roles
user.rb
has_one :user_role, dependent: :destroy
has_one :role, through: :user_role
has_one :company, through: :user_role
company.rb
has_many :user_roles, dependent: :destroy
has_many :users, through: :user_roles
has_many :roles, through: :user_roles
user_role.rb
belongs_to :user
belongs_to :role, optional: true
belongs_to :company
I want to create record using association and nested form and right now I am able to create Company along with user using nested form, but I also want to create user_role for User.
I have included accepts_nested_attributes_for :users in company model.
and used fields_for to create user in company new form.
The following is my form
<%= form_for @company, html: { multipart: true } do |f| %>
<% if company.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(company.errors.count, "error") %> prohibited this company from being saved:</h2>
<ul>
<% company.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field form-group">
<%= f.label :name %>
<%= f.text_field :name, class: :"form-control" %>
</div>
<div class="field form-group">
<%= f.label :website %>
<%= f.text_field :website, class: :"form-control" %>
</div>
<div class="field form-group">
<%= f.label :phone %>
<%= f.text_field :phone, class: :"form-control" %>
</div>
<div class="field form-group">
<%= f.label :description %>
<%= f.text_area :description, class: :"form-control" %>
</div>
<div class="field form-group">
<%= f.file_field :company_image %>
</div>
<%= f.fields_for :users do |builder| %>
<%= render "users_fields", :f => builder %>
<% end %>
<div class="actions">
<%= f.submit class: :'btn btn-default' %>
</div>
<% end %>
Right now, the user_role is not created when creating the company. I'm not sure how to proceed.
Any guidance will be appreciated. Thanks in advance.
user_roleis not created, so there must be some sort of error message or reason why it is not created. Include your controller code, include the output of@company.errors.full_messagesand the check how your parameters are showing up. Maybe theuser_roleis notsavingbecause it is not valid.user_rolehas abelongs_to :userand abelongs_to :role, optional: true. The validation of the presence ofrolewill be skipped, but you need to have a validuser. If you are giving thatuser_roleauser_idwhich belongs to an unsaved user, the validation will fail.params.require(:company).permit(:name, :website, :phone, :description, :company_image, users_attributes: [:email, :first_name, :last_name, :phone, :string, :birth_date, :join_date, :gender, :password, :password_confirmation])user_role belongs_to user.user_role.user_idmust correspond to theidof an existinguser. The fieldidof a row inusers. If you try to saveuser_roleand theuserhas not been already saved, you will trigger a validation error and theuser_rolewill not be saved. Save theuserbefore creating theuser_roledbwrong.user has_one :roleandrole has_many :users. You just set theuser.role_idfield when you create the user. No need to create the object, you create in yourseed.rbfile the standardrolesand if you need you provide functionality to create therolesseparately. When you create theuser, you just set the role he will have, not create an object. guides.rubyonrails.org/association_basics.html