【发布时间】:2020-11-20 23:39:38
【问题描述】:
为什么 CompaniesController 不使用 current_user.companies.build(company_params) 创建记录 CompanyUser?
如何在创建公司记录时创建 CompanyUser 记录?
型号:
用户
class User < ApplicationRecord
has_many :company_users, dependent: :destroy
has_many :companies, through: :company_users
end
公司
class Company < ApplicationRecord
include Userstampable::Stampable
has_many :company_users, dependent: :destroy
has_many :users, through: :company_users
accepts_nested_attributes_for :company_users, reject_if: :all_blank
validates :name, presence: true, length: { maximum: 100 }
validates :description, length: { maximum: 1000 }
end
公司用户
class CompanyUser < ApplicationRecord
include Userstampable::Stampable
belongs_to :company
belongs_to :user
validates :company_id, presence: true
validates :user_id, presence: true
end
CompaniesController
class CompaniesController < ApplicationController
def create
@company = current_user.companies.build(company_params)
respond_to do |format|
if @company.save
format.html { redirect_to @company, notice: 'Company was successfully created.' }
format.json { render :show, status: :created, location: @company }
else
format.html { render :new }
format.json { render json: @company.errors, status: :unprocessable_entity }
end
end
end
private
# Never trust parameters from the scary internet, only allow the white list through.
def company_params
params.require(:company).permit(:name, :description)
end
end
迁移
class CreateCompanyUsers < ActiveRecord::Migration[6.0]
def change
create_table :company_users do |t|
t.integer :role, default: 0
t.belongs_to :company, null: false, foreign_key: true
t.belongs_to :user, null: false, foreign_key: true
t.timestamps null: false, include_deleted_at: true
t.userstamps index: true, include_deleter_id: true
t.index [:company_id, :user_id], unique: true
end
end
end
【问题讨论】:
-
这能回答你的问题吗? - stackoverflow.com/a/9346642
-
current_user.companies.build(company_params) 应该创建一个带有 CompanyUser 记录的公司吗?
-
在 Rails 5+
belongs_to关联默认情况下是非可选的。 CompanyUser 中的这两个验证完全是多余的,可能是问题的根源。另外,您实际上是如何测试的? -
@max 我正在创建一家公司。如果我删除两个验证,仍然不会创建 CompanyUser 记录。
标签: ruby-on-rails