Rails 4使用_changed检查属性?

时间:2015-09-23 14:14:49

标签: ruby-on-rails ruby ruby-on-rails-4

我想检查用户是否更改了属性:_changed?

控制器:

def update
    respond_to do |format|
      if @incident.update(incident_params)
        if :tech_id_changed?
          @incident.update(evenement_type_user: 2)
          @incident.update(evenement_type_tech: 2)
        else
          @incident.update(evenement_type_user: 1)
          @incident.update(evenement_type_tech: 1)
        end

        format.html { redirect_to @incident, notice: 'Incident mis à jour.' }

        format.json { render :show, status: :ok, location: @incident }
      else
        format.html { render :edit }
        format.json { render json: @incident.errors, status: :unprocessable_entity }
      end
    end
  end

但是

if :tech_id_changed?

返回始终为true,即使tech_id没有更改。

为什么?

不,不行。据说我会更改事件的标题。我能做到吗?

      before_update :on_before_update

  def on_before_update
    if self.tech_id_changed?
      self.evenement_type_user = '2'
      self.evenement_type_tech = '2'
    else
      self.evenement_type_user = 1
      self.evenement_type_tech = 1
    end
  end

我的控制器中的更新功能将evenement_type_user重新归属为1:

  def update
    respond_to do |format|
      if @incident.update(incident_params)
        if @incident.tech_id_changed?
          @incident.update(evenement_type_user: 2)
          @incident.update(evenement_type_tech: 2)
        else
          @incident.update(evenement_type_user: 1)
          @incident.update(evenement_type_tech: 1)
        end

        format.html { redirect_to @incident, notice: 'Incident mis à jour.' }

        format.json { render :show, status: :ok, location: @incident }
      else
        format.html { render :edit }
        format.json { render json: @incident.errors, status: :unprocessable_entity }
      end
    end
  end

但如果我删除

        if @incident.tech_id_changed?
      @incident.update(evenement_type_user: 2)
      @incident.update(evenement_type_tech: 2)
    else
      @incident.update(evenement_type_user: 1)
      @incident.update(evenement_type_tech: 1)
    end

它有效,我忘了删除这段代码。

所以,非常感谢!!

3 个答案:

答案 0 :(得分:1)

根据Dirty文档,此行if :tech_id_changed?应为if @incident.tech_id_changed?

答案 1 :(得分:0)

:tech_id_changed?不是一种方法。如果您尝试:

:tech_id_changed?.class.name #=> Symbol

因此,您的测试将始终通过,因为Ruby中的虚假值仅为false本身和nil

你的意思是:

if @incident.tech_id_changed?
  ...
end

尝试在rails c下运行:

incident = Incident.new
incident.tech_id_changed? # => false
incident.tech_id = 999999
incident.tech_id_changed? # => true

同样修复您的模型:

class Incident < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :responses

  module Incidentmod
   attr_accessor :name, :content, :techidchanged
  end
  validates :title, :presence => true
  validates :content, :presence => true


  ## I'm not sure what you are doing
  ## This is an example of how to use it properly

  before_update :on_before_update


  def on_before_update
    if self.tech_id_changed?
      self.evenement_type_user = '2'
      self.evenement_type_tech = '2'
    else
      self.evenement_type_user = 1
      self.evenement_type_tech = 1
    end
  end
end

答案 2 :(得分:0)

@pavan:

我的模特:

    class Incident < ActiveRecord::Base
  belongs_to :user
  belongs_to :category
  has_many :responses

  module Incidentmod
    attr_accessor :name, :content, :techidchanged
  end
  validates :title, :presence => true
  validates :content, :presence => true

  after_update :techidchanged = true, :if => @incident.tech_id_changed?
end