1
votes

In my app users has many companies, and companies has many users, companies has many divisions and users have different division in different company for example User with id 1 belongs to Company 1 and Division 2 and to Company 2 Division 3 User.rb

class User < ActiveRecord::Base
  has_many :companies, through: :users_companies
  has_many :users_companies
  has_many :divisions, through: :users_divisions
  has_many :users_divisions
  has_many :users_roles, dependent: :destroy
  has_many :roles, through: :users_roles
end

Company.rb

class Company < ActiveRecord::Base

  #before_create :create_division

  has_many :users_companies
  has_many :users, through: :users_companies
  has_many :divisions, dependent: :destroy
  has_many :positions
end

Division.rb

class Division < ActiveRecord::Base
  has_ancestry
  belongs_to :company
  has_many :users, through: :users_divisions
  has_many :users_divisions
end

UsersCompany.rb

class UsersCompany < ActiveRecord::Base
  belongs_to :user
  belongs_to :company
end

UsersDivision.rb

class UsersDivision < ActiveRecord::Base
  belongs_to :user
  belongs_to :division
  belongs_to :company
end

UsersDivision table have structure id|user_id|division_id|company_id

when create all be fine

UsersDivision.create!(user: @user, division_id: params[:user][:division_ids], company_id: @company.id)

but when I try to update @user in table UsersDivision column company_id will be nill.

def update
    @company = Company.find(params[:company_id])
    @user = User.find(params[:id])

    if @user.update(user_params)
      redirect_to :back
    end
  end

How to pass params :company_id to update action?

<div class="edit-registration-container">
<%= @user.full_name %>
<%= form_for([@company, @user]) do |f| %>
<%= f.label :last_name, "Фамилия" %><br />
    <%= f.text_field :last_name, type: "text" %>

    <%= f.label :first_name, "Имя" %><br />
    <%= f.text_field :first_name, type: "text" %>

    <%= f.label :middle_name, "Отчество" %><br />
    <%= f.text_field :middle_name, type: "text" %>
    <%= f.label :division_ids, "Подразделение" %><br />
    <%= f.select :division_ids, @divisions_select %><br />
    <%= f.hidden_field :company_id, value: @company.id %>
    <%= f.fields_for :positions, @position do |ff| %>
    <%= ff.label :name, "Должность" %>
    <%= ff.text_field :name, type: "text" %>
    <%= ff.hidden_field :company_id, value: @company.id %>
    <% end %>
    <%= hidden_field_tag "user[role_ids][]", nil %>
    <% Role.all.each do |role| %>
      <%= check_box_tag "user[role_ids][]", role.id, @user.role_ids.include?(role.id), id: dom_id(role) %>
      <%= label_tag dom_id(role), role.name %><br>
      <% end %>
    <%= f.submit "Сохранить", class: "login loginmodal-submit", type: "submit"  %>
<% end %>
</div>
1
Can you post your view code please. - j-dexx
Yes of course, already added - adyl
form_for([@company, @user]) should pass the company id. Are you setting @company in the edit action? - j-dexx
Might want to look at user_params... Test it by printing it to see if company_id is there. If it's not, might not be permitted by strong params - Mingsheng
all promblem in users_divisions table after update action column company_id will be null - adyl

1 Answers

1
votes
  1. You need to permit that parameter to be accessible, so add it in your user controller params permit:

    params.require(:user).permit(YOUR PARAMETERS, {:company_id => []})

  2. Then you can call it using:

    params["user"]["company_id"]

  3. I think you can simply do this inside your update action:

    @company = Company.find(params["user"]["company_id"])

Here is my working example form:

<%= form_for :user, :url  => custom_users_path do |f| %>
    <%= f.text_field :name %>
    <%= f.hidden_field :company_id, :value => Company.find(1).id %> #4
<%= f.submit "Submit" %>

After I submit, inside my custom action my params["user"]["company_id"] would have this value: 4