Rails 4 app with has_many through

43 views
Skip to first unread message

Nikolay Lipovtsev

unread,
Apr 8, 2015, 11:47:59 PM4/8/15
to rubyonra...@googlegroups.com
I need to make an application in which there are two models: User and Company. In this case, users may be employees of the company, and then the user must be associated with Company model, and if the user is not an employee of the company, the connection with the Company model should not be.

I'm trying to make it through the association has_many through:

Models: 

# == Schema Information
#
# Table name: companies
#
# id :integer not null, primary key
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#


class Company < ActiveRecord::Base


  has_many
:company_users
  has_many
:users, through: :company_users
end


# == Schema Information
#
# Table name: users
#
#  id              :integer          not null, primary key
#  first_name      :string
#  last_name       :string
#  email           :string
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
#


class User < ActiveRecord::Base


  has_many
:company_users
  has_many
:companies, through: :company_users
end


# == Schema Information
#
# Table name: company_users
#
#  id         :integer          not null, primary key
#  company_id :integer
#  user_id    :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#


class CompanyUser < ActiveRecord::Base


  belongs_to
:company
  belongs_to
:user
end


Companies controller

class CompaniesController < ApplicationController


 
def signup
   
@company = Company.new
   
@user = @company.users.build
 
end


 
def create
   
@company = Company.new(company_params)
   
@company.save
 
end


 
private


 
def company_params
   
params.require(:company).permit(:name, users_attributes: [:first_name, :last_name, :email])
 
end
end


signup.html.erb

<%= form_for(@company) do |f| %>
 
<%= f.text_field :name %>        
 
<%= f.fields_for(@user) do |user_f| %>
   
<%= user_f.text_field :first_name %>
   
<%= user_f.text_field :last_name %>
 
<% end %>
 
<%= f.submit %>
<% end %>

But it does not work. Saved only instance of the model of the Company. How to association must be made in the models, and as should be done in the controller action Companies in creating a company with an employee?

Antônio Augusto Sousa Britto

unread,
Apr 10, 2015, 4:13:06 PM4/10/15
to rubyonra...@googlegroups.com
Try to set autosave: true on your association

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/15ecef57-0822-46ed-be32-146490dfa6b0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

dasibre

unread,
Apr 12, 2015, 1:45:46 PM4/12/15
to rubyonra...@googlegroups.com
I would test this out in the rails console first, to ensure the model relationships are correct. Also you might want to use :accepts_nested_attributes_for :users for your Company model
Reply all
Reply to author
Forward
0 new messages