ruby - Rails 3 validates inclusion of when using a find (how to proc or lambda) -
i've got project there currency , country table. there's price model requires valid currency , country code, have following validation:
validates :currency_code, :presence => true, :inclusion => { :in => currency.all_codes } validates :country_code, :presence => true, :inclusion => { :in => country.all_codes }
the all_codes method returns array of currency or country codes. works fine long no codes added table.
how write result of currency.all_codes either proc or inside lambda? tried proc.new { currency.all_codes } -- error object doesn't respond include?
note: answer true old versions of rails, rails 3.1 , above, procs accepted.
it must not accept procs. can use custom validation method same thing:
validate :currency_code_exists def currency_code_exists errors.add(:base, "currency code must exist") unless currency.all_codes.include?(self.currency_code) end
Comments
Post a Comment