当在 rails 中创建另一条记录时,使用外键创建关联记录
Create associated record with foreign keys when another record is created in rails
我在 rails 中有几个模型通过以下方式关联:
型号
class User < ApplicationRecord
has_many :contracts
has_many :properties, through: :contracts
has_many :documents, through: :contracts
class Contract < ApplicationRecord
belongs_to :seller_id, :class_name => 'Property'
belongs_to :buyer_id, :class_name => 'User'
has_many :documents
has_one :property
class Property < ApplicationRecord
has_many :contracts
has_many :users
class Document < ApplicationRecord
belongs_to :contract
has_many :users, through: :contracts
has_one :property, through: :contracts
创建属性的用户在创建时保存在seller_id下。这是我目前所拥有的:
控制器
class DocumentsController < ApplicationController
def create
@contract = Contract.new()
@document = current_user.documents.build(document_params)
if @document.save
flash[:notice] = "Successfully created document!"
redirect_to documents_path
else
flash[:alert] = "Error creating document!"
render :new
end
end
class ContractsController < ApplicationController
def create
@contract = Contract.new(contract_params)
if @contract.save
@contract.buyer_id << current_user
@contract.seller_id << Property.find(params[:seller_id])
@contract.property_id << Property.find(params[:id])
flash[:notice] = "Successfully created contract!"
else
flash[:alert] = "Error creating contract!"
end
end
class PropertiesController < ApplicationController
def create
@property = current_user.properties.build(property_params)
@property.user_id = current_user.id
@property.seller_id = current_user.id
if @property.save
flash[:success] = "Successfully created property!"
redirect_to property_path(@property)
else
flash[:alert] = "Error creating new property!"
render :new
end
end
我的问题
如何自动创建合同记录,将 buyer_id 关联到当前用户,将 seller_id 关联到 属性 的卖家,将 property_id 关联到属性 当我在表单中创建新文档时?
请彻底,我是新手。
谢谢
class Contract < ApplicationRecord
belongs_to :buyer, :class_name => 'User', foreign_key: buyer_id
has_many :documents
belongs_to :property, foreign_key: seller_id #belongs_to is used to define the relationship if foreign_key to the associated model is present.
#Creating Contract
con = Contract.new(contract_params)
con.buyers << User.find(current_user.id) # links user to contract via. buyer_id
con.property << Property.find(params[:property_id]) # links property to contract via seller_id
con.save!
#Creating Document
doc = Document.new(document_params)
doc.contracts << con # links contract to document
doc.save!
我在 rails 中有几个模型通过以下方式关联:
型号
class User < ApplicationRecord
has_many :contracts
has_many :properties, through: :contracts
has_many :documents, through: :contracts
class Contract < ApplicationRecord
belongs_to :seller_id, :class_name => 'Property'
belongs_to :buyer_id, :class_name => 'User'
has_many :documents
has_one :property
class Property < ApplicationRecord
has_many :contracts
has_many :users
class Document < ApplicationRecord
belongs_to :contract
has_many :users, through: :contracts
has_one :property, through: :contracts
创建属性的用户在创建时保存在seller_id下。这是我目前所拥有的:
控制器
class DocumentsController < ApplicationController
def create
@contract = Contract.new()
@document = current_user.documents.build(document_params)
if @document.save
flash[:notice] = "Successfully created document!"
redirect_to documents_path
else
flash[:alert] = "Error creating document!"
render :new
end
end
class ContractsController < ApplicationController
def create
@contract = Contract.new(contract_params)
if @contract.save
@contract.buyer_id << current_user
@contract.seller_id << Property.find(params[:seller_id])
@contract.property_id << Property.find(params[:id])
flash[:notice] = "Successfully created contract!"
else
flash[:alert] = "Error creating contract!"
end
end
class PropertiesController < ApplicationController
def create
@property = current_user.properties.build(property_params)
@property.user_id = current_user.id
@property.seller_id = current_user.id
if @property.save
flash[:success] = "Successfully created property!"
redirect_to property_path(@property)
else
flash[:alert] = "Error creating new property!"
render :new
end
end
我的问题
如何自动创建合同记录,将 buyer_id 关联到当前用户,将 seller_id 关联到 属性 的卖家,将 property_id 关联到属性 当我在表单中创建新文档时?
请彻底,我是新手。
谢谢
class Contract < ApplicationRecord
belongs_to :buyer, :class_name => 'User', foreign_key: buyer_id
has_many :documents
belongs_to :property, foreign_key: seller_id #belongs_to is used to define the relationship if foreign_key to the associated model is present.
#Creating Contract
con = Contract.new(contract_params)
con.buyers << User.find(current_user.id) # links user to contract via. buyer_id
con.property << Property.find(params[:property_id]) # links property to contract via seller_id
con.save!
#Creating Document
doc = Document.new(document_params)
doc.contracts << con # links contract to document
doc.save!