【问题标题】:save current_user.id into another column将 current_user.id 保存到另一列
【发布时间】:2016-06-24 15:13:54
【问题描述】:

目前我有模型commercial_group,其中有created_by 和updated_by 列。它属于用户,一个用户将拥有commercial_groups。现在,我想每当用户创建商品组时,他的 id 将保存到 created_by 中,并且每当有人更新商品组时,他的 id 将保存到字段 update_by 中。 目前,我收到此错误:

CommodityGroup 的未知属性“user_id”。

基本上,我不想将列 user_id 添加到商品组表中,因为它与列 created_by 相同。因此,有人可以在这里指导我一点。这是我的文件:

commodity_groups_controller.rb:

class CommodityGroupsController < ApplicationController
  before_action :set_commodity_group, only: [:show, :edit, :update, :destroy]

  # GET /commodity_groups
  def index
    @commodity_groups = CommodityGroup.all
  end

  # GET /commodity_groups/1
  def show
  end

  # GET /commodity_groups/new
  def new
    @commodity_group = current_user.commodity_groups.build
  end

  # GET /commodity_groups/1/edit
  def edit
  end

  # POST /commodity_groups
  def create
    @commodity_group = current_user.commodity_groups.build(commodity_group_params)


    if @commodity_group.save
      redirect_to commodity_groups_path, notice: init_message(:success, t('message.new_success', page_name: t('page_name.commodity_group')))
    else
      redirect_to new_commodity_group_path, notice: init_message(:error, t('message.new_error', page_name: t('page_name.commodity_group')))
    end
  end

  # PATCH/PUT /commodity_groups/1
  def update
    if @commodity_group.update(commodity_group_params)
      redirect_to @commodity_group, notice: 'Commodity group was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /commodity_groups/1
  def destroy
    @commodity_group.destroy
    redirect_to commodity_groups_url, notice: 'Commodity group was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_commodity_group
      @commodity_group = CommodityGroup.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def commodity_group_params
      params[:commodity_group]
    end
end

commodity_group.rb

class CommodityGroup < ActiveRecord::Base
  extend FriendlyId
  friendly_id :code, use: :history

  belongs_to :user_created,
             class_name: 'User',
             primary_key: :id,
             foreign_key: :created_by
  belongs_to :user_updated,
             class_name: 'User',
             primary_key: :id,
             foreign_key: :updated_by

  validates_presence_of :code
  validates_presence_of :name
  validates_presence_of :user
end

user.rb:

class User < ActiveRecord::Base
  extend FriendlyId
  include Filterable

  friendly_id :slug_candidates, use: :history
  has_secure_password
  acts_as_paranoid

  has_many :activities
  has_many :pricing_histories
  has_many :commodity_groups
end

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    CommodityGroup 正在commodity_groups 表中搜索user_id

    您需要在 User 模型中指定。

    class User < ActiveRecord::Base
      extend FriendlyId
      include Filterable
    
      friendly_id :slug_candidates, use: :history
      has_secure_password
      acts_as_paranoid
    
      has_many :activities
      has_many :pricing_histories
      has_many :commodity_groups_created, class_name: 'CommodityGroup',
                                          foreign_key: :created_by
      has_many :commodity_groups_updated, class_name: 'CommodityGroup',
                                          foreign_key: :updated_by
    end
    

    【讨论】:

    • 感谢您的帮助。即使我仍然无法将 current_user.id 保存到表商品组中的 created_by 列,我仍然想将您的解决方案标记为最佳解决方案。我将针对我当前的问题提出另一个问题。祝你有美好的一天。
    【解决方案2】:

    您已经在 CommodityGroup 端获得了 foreign_key,您只需要将它也添加到 User 端,您应该很好,例如:

    has_many :commodity_groups, class_name: 'User', foreign_key: :created_by
    

    (您可能需要仔细研究细节以确定还需要添加什么,但这基本上会给您提供解决方法的想法)

    【讨论】:

    • 感谢您的建议。那么,这意味着我还必须将 created_by 字段添加到用户表中?
    • 不 - created_by 字段已经存在于另一张表上...但默认情况下,Rails 将使用类名来确定字段名,在这种情况下,它将寻找一个名为 @987654323 的字段@ - 你的另一张桌子上不存在它......所以你必须告诉它字段名称到底是什么
    • 是的,我明白你的意思。实际上,Deepack 先生已经给出了我正在寻找的解决方案,非常感谢。祝你有美好的一天。
    猜你喜欢
    • 2019-03-01
    • 1970-01-01
    • 2018-05-05
    • 2018-01-22
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多