ruby on rails - nested validation - model -
i have 2 linked models (simplified) :
ad :
class ad < activerecord::base has_many :propositions validates :pricemin validates :pricemax ...
proposition :
class proposition < activerecord::base belongs_to :ad attr_accessible :price_proposition ...
i'm trying add restriction price_proposition (in "proposition" model) between "pricemin" , "pricemax".
how ?
thanks help
client side validation via javascript 1 way go. if want server side validation, can this:
class proposition < activerecord::base belongs_to :ad attr_accessible :price_proposition validate :price_proposition_in_range def price_proposition_in_range pricemin = self.ad.pricemin pricemax = self.ad.pricemax if self.price_proposition < pricemin || self.price_proposition > pricemax errors.add(:price_proposition, "must between #{pricemin} , #{pricemax}") end end . . . end
and of course, can add further checks in method, depending on whether want allow blank or not etc etc.
Comments
Post a Comment