mardi 24 novembre 2015

validation using real named scopes rails

I have an invoice model with approver_note, po_number and state_type.

I need validations to check:

scope :approver, where(state_type: 3)
scope :po_no, where(state_type: 2)

validates :approver_note, :presence => true, uniqueness: { scope: [:ac_id, :approver]} if: :state_three?
validates :po_number, :presence => true, uniqueness: { scope: [:ac_id, :po_no]} if: :state_two?

def state_three?
    self.state_type==3
end

def state_two?
    self.state_type==2
end

How can I make sure that the uniqueness in approver_note validator is run on selected scope of records. It should validate using records having state_type=3.

I need something in the similar lines of this bug...

http://ift.tt/1OpxLU4

Is this available in rails now? or can we achieve this using custom validation?

Aucun commentaire:

Enregistrer un commentaire