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 ?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Erwin at Mar 22, 2012 at 9:45 pm
    ’SOLVED' I totally forgot to look at the 'old' acts_as_a_list , now a
    plugin ...!
    On Mar 22, 7:32 pm, Erwin wrote:
    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 ?
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedMar 22, '12 at 6:32p
activeMar 22, '12 at 9:45p
posts2
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Erwin: 2 posts

People

Translate

site design / logo © 2021 Grokbase