【发布时间】:2017-02-10 15:47:24
【问题描述】:
所以我有一个具有多对多关系的用户模型和合同模型。合同属于多个用户,但具有不同的 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
我需要让每个选择器更新所选用户,但只有最后一个在工作,我不知道如何让它工作。
我觉得这种方法可行,但我是 Rails 新手,也许我使用了错误的方法。 我尝试了一个简单的 has_and_belongs_to_many 关系,在“构建复杂表单”下创建像 http://guides.rubyonrails.org/form_helpers.html 解释的用户,但是当我使用字段设置它们时,我失去了表单中每个特定用户之间的区别
希望我足够清楚,谢谢!
【问题讨论】:
-
你能把你的模型定义也去掉吗?
-
刚刚编辑了问题
-
让我知道我的建议是否有效,我很好奇如果不使用复数和 has_many 这样做。
标签: ruby-on-rails many-to-many erb nested-forms belongs-to