2016-06-23 8 views
0

私はモデルStudentProductRelationshipを持っています。私はカスタムバリデータを追加していRailsで検証メソッドが呼び出されるのはいつですか?

validate :validate_primary_product , :if => "!primary_product"

方法は

def validate_primary_tag 
    unless StudentProductRelationship.exists?(:primary_product => true, :student_id => student_id) 
    errors.add(:base,"There is no primary product associated to product") 
    else 
    end 
end 

primary_productがブールフィールドです。少なくともprimary_productの存在がstudent_idであることを検証したいと思います。問題は、StudentProductRelationshipオブジェクトがsprprimary_product = trueといっているとします。私がすればspr.update_attributes(primary_product: false)StudentProductRelationship.exists?(:primary_product => true, :student_id => student_id)が存在するため、この検証ではエラーが発生しませんsprはまだdbにprimary_product = trueで存在します。どのように私はこれを上回るのですか?

答えて

0

validates_presence_of :primary_product, scope: :student_idはあなたのために働きませんか?

+0

いいえ存在しない私はそれが真実である必要があります。これは正しく 'false'を扱うでしょうか? – Codernx123

関連する問題