簡體   English   中英

將參數值保存在before_save回調中

[英]Save params value in before_save callback

你好,我在我的參數

參數:{“ utf8” =>“✓”,“ authenticity_token” =>“ YmYvS / vPVB9FD7 + XvDx0 + K8UKHReHLifTmc91xD8pASSNqg9d0b / 77NnodxQYhbN806661eSBk9vQwTHib3w / w =”“ =”“>” “ dinshaw”,“ date_of_joining” =>“ 2015年6月8日”,“ date_of_birth” =>“ 2015年6月2日”,“周年紀念日” =>“ 2015年6月3日”,“ last_position” =>“開發人員”,“ password_no“ =>” AK972345“,” passport_expiry_date“ =>” 2015年6月27日“,” pan_no“ =>” BHJ47“},”電子郵件“ =>” draje@adroit-inc.com“,”角色“ => “ SUPER-ADMIN”,“ department_id” =>“ 1”,“ designation_id” =>“ 1”,“ is_active” =>“ 1”,“ contact_attributes” => {“ phone_no” =>“ 9856321470”,“ current_address1” “ =>” test1dinsh“,” current_address2“ =>” awwwwwwwww“,”國家“ =>” IN“,”城市“ =>”印多爾“,” zip“ =>” 452001“,”緊急聯絡人“ =>”阿比“,” emergency_contact_no“ =>” 9632145870“,” relation“ =>” farji“},” permanent_address_attributes“ => {” address1“ =>” testbghghgjh“,” address2“ =>” hdvhdkhfdhnbdfds“,”國家“ => “ IN”,“ city” =>“ Ghaziabad”,“ zip” =>“ 12356”}},“ contact” => {“ state_code” =>“ MP”},“ permanent_address” => {“ state_code” =>“ CH”},“ commit” =>“ Update Employee”,“ id” =>“ 1”}

現在,在我的employee.rb文件中,我已包含此代碼

class Employee < ActiveRecord::Base
  belongs_to :department
  belongs_to :designation
  has_one :profile
  has_one :contact
  has_one :permanent_address
  has_many :attachments, as: :attachable
  accepts_nested_attributes_for :attachments
  accepts_nested_attributes_for :profile, :contact, :permanent_address
  before_save :set_state_code

  def set_state_code
    self.contact.state_code = 'AP'
    self.permanent_address.state_code = 'MP'
  end

  def create_association_instance
    self.build_profile unless self.profile
    self.build_contact unless self.contact
    self.build_permanent_address unless self.permanent_address
    self.attachments.build unless self.attachments.present?
  end

end

但我想要

self.contact.state_code = params[:contact][:state_code] && 
self.permanent_address.state_code = params[:permanent_address][:state_code] 

我沒有如何在模型中獲取它。 請指導我。 提前致謝

class Employee < ActiveRecord::Base
  attr_accessor :contact_state_code, :address_state_code
  before_save :set_state_code

  def set_state_code
    self.contact.update_column(state_code: self.contact_state_code)
    self.permanent_address.update_column(state_code: self.address_state_code)
  end
end

並在控制器中執行以下操作:

params[:employee][:contact_state_code] = params[:contact][:state_code]
params[:employee][:address_state_code] = params[:permanent_address][:state_code] 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM