【问题标题】:Multiple belongs_to to same model only updating for last id in form多个属于同一模型仅更新表单中的最后一个 id
【发布时间】: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


【解决方案1】:

好的,所以您的模型定义存在一些问题。

  1. 如果您使用has_many,则您的关系必须是复数,所以它将是has_many :contracts。这很可能是导致您的问题的原因。
  2. 对同一个表使用多个外键并不是最好的主意。而是有一个has_many :contracts, foreign_key: 'contractor_id'。但随后使用单独的变量来定义User 的作用。在Contract 中,您将拥有belongs_to :user, :foreign_key =&gt; 'contractor_id'。然后,如果您想调用 creatorleader 等,您可以指定自定义 accessor_methods

【讨论】:

  • Contracts 在我的代码中是复数形式,在这里用更简单的术语重写时出现了拼写错误。我了解您对角色变量和 accessor_methods 的建议,但我需要在合同和用户之间保持多对多关系。我的合同将始终关联 4 个用户
  • 如果我也没有这些 ID,我不确定我的视图应该如何工作。你能给我更详细的 sn-ps 我应该如何实现这一切吗?
  • 好的,我要为你的情况写一个完整的解决方案,我需要你添加你的控制器和整个视图以获得更好的图片。再想一想,我确实认为你需要 has_and_belongs_to_many,所以我认为你没有找到区分用户的方法是你让它发挥作用所缺少的一步。
  • 我在问题中添加了控制器和完整表格
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
相关资源
最近更新 更多