Ruby on rails 属于多个模型的模型的CRUD方法

Ruby on rails 属于多个模型的模型的CRUD方法,ruby-on-rails,ruby,activerecord,associations,model-associations,Ruby On Rails,Ruby,Activerecord,Associations,Model Associations,我是Rails新手,这里可能缺少一些非常基本的东西: 用户可以为公司的分支机构和部门创建联系人 Branch.rb class Branch < ApplicationRecord belongs_to :company has_many :contacts end class Division < ApplicationRecord belongs_to :company has_many :contacts end class Contact

我是Rails新手,这里可能缺少一些非常基本的东西:

用户可以为公司的分支机构和部门创建联系人

Branch.rb

class Branch < ApplicationRecord
    belongs_to :company
    has_many :contacts
end
class Division < ApplicationRecord
    belongs_to :company
    has_many :contacts
end
class Contact < ApplicationRecord
    belongs_to :branch
    belongs_to :division
end
resources :companies, :shallow => true do
    get 'company_page'
    resources :branches, :shallow => true do
        get 'branch_page'
        resources :contacts
    end 
    resources :divisions, :shallow => true do
        get 'division_page'
        resources :contacts
    end 
end 
因此,如果我从分支或部门创建联系人,它将转到contacts#create method

在my contacts_controller.rb中,我有:

def create
    @newContact = Contact.new(contact_params)
    id = @division = @branch = nil
    isBranch = false
    if params[:branch_id] != nil
        isBranch = true
        id = params[:branch_id]
    else
        isBranch = false
        id = params[:division_id]
    end 
    if isBranch
      branch = Branch.find(id) 
      @newContact.branch = branch
      @branch = branch
    else
      division = Division.find(id) 
      @newContact.division = division
      @division = division
    end  

    respond_to do |format|
        if @newContact.save
          format.js
          format.html { render :nothing => true, :notice => 'Contact created successfully!' }
          format.json { render json: @newContact, status: :created, location: @newContact }
        else
          format.html { render action: "new" }
          format.json { render json: @newContact, status: :unprocessable_entity }
        end
    end     
end
但是我在
@newContact.save期间遇到了
ActiveRecord错误


我确信我在这里做了一些根本上非常错误的事情,Rails以另一种我不知道的优雅方式处理这些事情。

正如@Anthony所指出的,您需要使您的
属于
关联可选:

# app/models/contact.rb

class Contact < ApplicationRecord
  belongs_to :branch, optional: true
  belongs_to :division, optional: true
end
这证明它是有效的:

# spec/controllers/contacts_controller_spec.rb

require 'rails_helper'

RSpec.describe ContactsController, type: :controller do
  let(:company) { Company.create!(name: 'Company Name') }
  let(:division) { Division.create!(name: 'Division Name', company: company) }
  let(:branch) { Branch.create!(name: 'Branch Name', company: company) }

  describe '#create' do
    context 'when created with a division id' do
      let(:attributes) { {'division_id' => division.id, 'name' => 'Contact Name'} }

      it 'creates a contact record and associates it with the division' do
        expect(Contact.count).to eq(0)
        post :create, params: {contact: attributes}

        expect(Contact.count).to eq(1)
        contact = Contact.first
        expect(contact.division).to eq(division)
      end
    end

    context 'when created with a branch id' do
      let(:attributes) { {'branch_id' => branch.id, 'name' => 'Contact Name'} }

      it 'creates a contact record and associates it with the branch' do
        expect(Contact.count).to eq(0)
        post :create, params: {contact: attributes}

        expect(Contact.count).to eq(1)
        contact = Contact.first
        expect(contact.branch).to eq(branch)
      end
    end
  end
end

错误是什么?
在265ms内完成了500个内部服务器错误(ActiveRecord:66.8ms)
我猜错误是因为
联系人
有两个属于关联,但您只给了一个。log/development.log中的错误是什么?是的,这一点您是正确的。我想知道怎么处理。问题是
@newContact.save为false
,因此它将转到
respond|u to do | format |块
的else块。您可以执行
属于:分支,可选:true;属于:除法,可选:true
# spec/controllers/contacts_controller_spec.rb

require 'rails_helper'

RSpec.describe ContactsController, type: :controller do
  let(:company) { Company.create!(name: 'Company Name') }
  let(:division) { Division.create!(name: 'Division Name', company: company) }
  let(:branch) { Branch.create!(name: 'Branch Name', company: company) }

  describe '#create' do
    context 'when created with a division id' do
      let(:attributes) { {'division_id' => division.id, 'name' => 'Contact Name'} }

      it 'creates a contact record and associates it with the division' do
        expect(Contact.count).to eq(0)
        post :create, params: {contact: attributes}

        expect(Contact.count).to eq(1)
        contact = Contact.first
        expect(contact.division).to eq(division)
      end
    end

    context 'when created with a branch id' do
      let(:attributes) { {'branch_id' => branch.id, 'name' => 'Contact Name'} }

      it 'creates a contact record and associates it with the branch' do
        expect(Contact.count).to eq(0)
        post :create, params: {contact: attributes}

        expect(Contact.count).to eq(1)
        contact = Contact.first
        expect(contact.branch).to eq(branch)
      end
    end
  end
end