Rails 4 Custom Validations -


i have problem custom validator. have model price, looks this:

class price < activerecord::base   belongs_to :car   validates :from_days, :to_days, :netto_price, presence: true, numericality: true   validate :days_range_validation    private    def days_range_validation     unless to_days > from_days        errors[:to_days] << i18n.t('price.must_be_greater')     end   end end 

and problem when leave to_days , from_days blank in form, following error:

undefined method `>=' nil:nilclass 

and goal use validator when from_days , to_days present, don't know how that. ideas?

you should set :if option:

validate :days_range_validation, :if => :days_ranges_present?  # ... private  def days_ranges_present?   to_days.present? && from_days.present? end 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -