在保存回调之前访问嵌套属性

时间:2016-12-22 10:40:38

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

我有以下关系

class Customer < ActiveRecord::Base
  has_many :applications, :dependent => :destroy 
  before_save: validate_presence_priority_id
end

在我的客户控制器中我有

def customer_params
params.require(:customer).permit(:name, application_attributes: [:duration, :priority_id, :_destroy])
end

现在我想做这样的事情

def validate_presence_priority_id((attributes))
    if !attributes['priority_id'].present?
      attributes.merge!({:_destroy => 1})
    end
end

所以基本上我想检查priority_id是否不存在然后只添加destroy属性。我怎样才能做到这一点?

0 个答案:

没有答案