1
私は、次の方法で関連付けられているレールにいくつかのモデルを持っている:外部キーに関連付けられたレコードを作成します
モデル
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
私の質問
自動的に売り手に、現在のユーザーにseller_idをbuyer_idを関連付ける契約レコードを作成する方法をフォームに新しい文書を作成すると、プロパティのproperty_idがプロパティに追加されますか?
私はかなり新しいです。
は