所以我有一个用户模型,并与许多合同模型一对多的关系。合同属于多个用户,但用不同的ID,所以我能做到这一点多belongs_to的,以相同的模型只更新了最后一个ID形式
Contract.find(params[:id]).creator.email
Contract.find(params[:id]).leader.email
Contract.find(params[:id]).buyer.email
Contract.find(params[:id]).seller.email
User.find(params[:id]).contracts
在我的表单我有这个,所以我可以每个ID设置为已创建的用户
<%= simple_form_for(@contract) do |f| %>
<% if @contract.errors.any? %>
<div id="error_explanation">
<h3><%= pluralize(@contract.errors.count, 'error') %> prohibited this contract from being saved:</h3>
<ul>
<% @contract.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :creator_id %><br>
<%= f.select(:creator_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<div class="form-group">
<%= f.label :leader_id %><br>
<%= f.select(:leader_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<div class="form-group">
<%= f.label :buyer_id %><br>
<%= f.select(:buyer_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<div class="form-group">
<%= f.label :seller_id %><br>
<%= f.select(:seller_id, User.all.map { |c| [c.email, c.id] }) %>
</div>
<% end %>
我的合同是与每个用户ID设置正确创建,但是当我检查用户的合同,我只能找到相同的合同,如果用户是卖方(这样的形式的最后一个div)
编辑: 这里是我的两个车型。
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :contract, :foreign_key => 'creator_id'
has_many :contract, :foreign_key => 'leader_id'
has_many :contract, :foreign_key => 'buyer_id'
has_many :contract, :foreign_key => 'seller_id'
validates :email, presence: true, uniqueness: true
validates :role, presence: true
end
class Contract < ApplicationRecord
belongs_to :creator, :class_name => 'User', :foreign_key => 'creator_id'
belongs_to :leader, :class_name => 'User', :foreign_key => 'leader_id'
belongs_to :buyer, :class_name => 'User', :foreign_key => 'buyer_id'
belongs_to :seller, :class_name => 'User', :foreign_key => 'seller_id'
end
合同控制器:
class ContractsController < ApplicationController
before_action :set_contract, only: [:show, :edit, :update, :destroy]
def index
@contracts = Contract.all
end
def show
@contract = Contract.find(params[:id])
end
def new
@contract = Contract.new
end
def edit
end
def create
@contract = Contract.new(contract_params)
if @contract.save
redirect_to contracts_path, notice: 'Le contract a été créé avec succès'
else
render :new
end
end
def update
if @contract.update(contract_params)
redirect_to contract_path
else
render :edit
end
end
def destroy
@contract.destroy
redirect_to contracts_path
end
private
def set_contract
@contract = Contract.find(params[:id])
end
def contract_params
params.require(:contract).permit(:creator_id,
:leader_id,
:buyer_id,
:seller_id)
end
end
我需要每个选择更新选择的用户,但只有最后一个是工作的,我无法弄清楚如何使它发挥作用。
我觉得这种方法可以工作,但我在新的轨道,也许我用错了方法。 我想一个简单的has_and_belongs_to_many关系,制造出用户喜欢在这里解释下http://guides.rubyonrails.org/form_helpers.html“建立复杂的形式”,但我的形式失去了每个特定用户之间的区别,当我与现场的
希望将它们设置我清楚够了,谢谢!
您是否也可以删除模型定义? –
刚刚编辑问题 – LRP
让我知道如果我的建议工作,我很好奇,如果不使用has_many复数做到这一点。 –