Fatima
Fatima

Reputation: 355

save current_user.id into another column

currently I have the model commodity_group, there are created_by, and updated_by columns in it. It belongs to user, and one user will have commodity_groups. Now, I want to whenever a user created a commodity group, his id will be saved into the created_by, and whenever somebody update a commodity group, his id will be saved into the field update_by. At the moment, i get this error:

unknown attribute 'user_id' for CommodityGroup.

basically, I don't want to add the column user_id to the commodity_group table since it is the same with the column created_by. Therefore, could somebody guide me here a little bit. Here are my files:

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

Upvotes: 0

Views: 199

Answers (2)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

CommodityGroup is searching for user_id in commodity_groups table.

You need to specify that in the User model.

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

Upvotes: 0

Taryn East
Taryn East

Reputation: 27747

you've got the foreign_key on the CommodityGroup side, you just need to add it to the User side as well and you should be good to go eg:

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

(you may need to jigger with the details to figure out what else you need to add, but this will basically give you the idea on how to solve it)

Upvotes: 0

Related Questions