Hoang Hoang - 3 months ago 14
Ruby Question

Rails: auto update reference table when update foreign_key

When i try to update foreign_key for table BusinessCard. It auto update the Company table without needed.

My Console Image

Can someone help me with this. Thanks

Edit---



My Controller

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 Model

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


Company Model

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


Company Migration

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

Answer

Try this:

instead this in BusinnesCard Model

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

put this

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

PS Your code is little diry, i recommend you to replace primary_keys. For example, instead company_id, use id

Comments