Rails:更新foreign_key时自动更新引用表

时间:2016-08-30 05:50:30

标签: ruby-on-rails ruby

当我尝试更新表BusinessCard的foreign_key时。它会在不需要的情况下自动更新公司表。

My Console Image

有人可以帮我解决这个问题。感谢

编辑---

我的控制器

def update
  @bc = BusinessCard.find_by(business_card_id: params[:id], deleted: false)
  raise "名刺情報は存在しておりません。" unless @bc
  raise "同じ名刺情報が存在しております。" if (@bc.name != params[:name] or @bc.email != params[:email] or @bc.company.name != params[:c_name]) and BusinessCard.joins(:company).where(name: params[:name], email: params[:email], deleted: 0, 'companies.name' => params[:c_name]).exists?

  ActiveRecord::Base.transaction do
    #@bc.name = params[:name]
    #@bc.email = params[:email]
    #@bc.tel = params[:tel]
    #@bc.furigana = params[:furigana]
    #@bc.recieve_date = params[:recieve_date]
    #@bc.update_by = @current_user.user_id

    #Company
    @bc.company_id = bz_company if params[:c_name] and params[:c_post_code]
    #department
    #@bc.department_id = bz_department if params[:d_name]

    raise @bc.errors unless @bc.save

    #images
    #bz_omt if params[:i_omt]
    #bz_ura if params[:i_ura]
  end

  render_json(@bc, :ok)
end


def bz_company
  @company = Company.find_by(name: params[:c_name], post_code: params[:c_post_code])
  @company = Company.new(name: params[:c_name], post_code: params[:c_post_code], create_by: @current_user.user_id) unless @company
  @company.address = params[:c_address]
  @company.email = params[:c_email]
  @company.tel = params[:c_tel]
  @company.fax = params[:c_fax]
  @company.url = params[:c_url]
  @company.deleted = 0
  @company.update_by = @current_user.user_id

  raise @company.errors unless @company.save
  @company.company_id
end

BusinessCard模型

class BusinessCard < ApplicationRecord

  #Association
  #With Tag
  has_many :map_tags, primary_key: 'business_card_id', foreign_key: 'business_card_id'
  has_many :tags, :through => :map_tags
  #with Comment
  has_many :map_comments, primary_key: 'business_card_id', foreign_key: 'business_card_id'
  has_many :comments, :through => :map_comments
  #with Company
  has_one :company, primary_key: 'company_id', foreign_key: 'company_id'
  #with department
  has_one :department, primary_key: 'department_id', foreign_key: 'department_id'
  #with file_locations
  has_many :file_locations, primary_key: 'business_card_id', foreign_key: 'business_card_id'

 end

公司模式

 class Company < ApplicationRecord
   #Association
   has_many :business_cards, primary_key: 'company_id', foreign_key:  'company_id' 
 end

公司迁移

class CreateCompanies < ActiveRecord::Migration[5.0]
  def change
    create_table :companies, id: false do |t|
      t.column :company_id, 'INTEGER PRIMARY KEY AUTOINCREMENT'
      t.string :name, limit: 150, null: false
      t.text :address, limit: 1000, null: false
      t.string :email, limit: 129
      t.string :tel, limit: 20
      t.string :fax, limit: 20
      t.string :url, limit: 150

      t.boolean :deleted, default: false
      t.integer :create_by
      t.integer :update_by
      t.timestamps
    end
  end
end

BusinessCard Migration

class CreateBusinessCards < ActiveRecord::Migration[5.0]
  def change
    create_table :business_cards, id: false do |t|
      t.column :business_card_id, 'INTEGER PRIMARY KEY AUTOINCREMENT'
      t.string :name, limit: 50, null: false
      t.string :furigana, limit: 50
      t.string :email, limit: 129, null: false
      t.string :tel, limit: 20, null: false
      t.integer :owner_id
      t.datetime :recieve_date

      t.integer :company_id, null: false
      t.integer :department_id, null: false

      t.boolean :deleted, default: false
      t.integer :create_by
      t.integer :update_by
      t.timestamps
    end

    add_index :business_cards, :business_card_id, :unique => true
  end
end

2 个答案:

答案 0 :(得分:0)

试试这个:

而不是在BusinnesCard模型

has_one :company, primary_key: 'company_id', foreign_key: 'company_id'

把这个

belongs_to :company, primary_key: 'company_id', foreign_key: 'company_id'

PS你的代码很小,我建议你替换primary_keys。例如,而不是company_id,请使用id

答案 1 :(得分:0)

您正在使用不遵循Rails约定的主键列名称。因此,您必须在模型中告诉Rails primary_key=的名称:

# in app/models/company.rb
self.primary_key = 'company_id'

# in app/models/business_card.rb
self.primary_key = 'business_card_id'