Erwin
unread,Mar 22, 2012, 2:32:38 PM3/22/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to Ruby on Rails: Talk
I am trying to write a validation for a specific use case
when an admin wants to change a project payment terms, I display a
form listing the existing payment terms for this project in sequence
1 rate: 30% label ..
2 rate: 30% label ..
3 rate: 40% label ..
An admin should be able to add a new payment_term ( add_association
in nested form) or delete an existing one..
but also re-order the sequence.... i.e change 1=> 3 and 3 => 1
I am trying to set up a validation, so a seq number cannot be created
twice , but it doesn't work ...
Project
has_many :payment_terms
accepts_nested_attributes_for :payment_terms, :reject_if
=> :all_blank, :allow_destroy => true
attr_accessible :payment_terms_attributes
PaymentTerm
belongs_to :project
attr_accessible :seq, :rate, ....
validates :term_seq, :numericality => { :greater_or_equal_to => 0 }
validates :rate, :numericality => { :greater_than =>
0.0, :less_than_or_equal_to => 100.0 }
obviously a :seq attribute should be unique within the scope of the
project, so I tried :
validates :seq, :uniqueness => { :scope => :project_id, :message =>
"Seq.?" }
BUT, updating the payment terms
id: 23 :seq 1 rate: 30%
id: 24 :seq 2 rate: 30%
id: 25 :seq 3 rate: 40%
to ( as an example)
id: 23 :seq 2 rate: 30% <= first record to update
id: 24 :seq 3 rate: 30%
id: 25 :seq 1 rate: 40%
doesn't work at all.... updating id: 23 raises a validation error
as :seq 2 is already existing in record :24
so scoping is not the solution, is this a bad design I could change ?