首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将current_user.id保存到另一列

将current_user.id保存到另一列
EN

Stack Overflow用户
提问于 2016-06-24 07:08:05
回答 2查看 184关注 0票数 0

目前我有模型commodity_group,其中有created_by和updated_by列。它属于用户,一个用户将拥有commodity_groups。现在,我希望每当用户创建商品组时,他的id将保存到created_by中,每当有人更新商品组时,他的id就会保存到字段update_by中。现在,我得到了一个错误:

未知属性“user_id”用于CommodityGroup。

基本上,我不想将列user_id添加到commodity_group表中,因为它与列created_by相同。所以,有人能在这里给我指点吗。这是我的档案:

commodity_groups_controller.rb:

代码语言:javascript
复制
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

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-24 07:13:54

CommodityGroup正在commodity_groups表中搜索user_id

您需要在User模型中指定这一点。

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2016-06-24 07:11:32

您已经在foreign_key端获得了CommodityGroup,您只需要将它添加到用户端,就可以了:

代码语言:javascript
复制
has_many :commodity_groups, class_name: 'User', foreign_key: :created_by

(您可能需要了解详细信息,以确定您还需要添加哪些内容,但这将给您提供如何解决该问题的基本思路)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38007543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档