ruby on rails - How do i improve this method -


is there better way this. looking alternate way solve problem

def validate_work_dates     if ld_work_start_date.present? && off_work_date.present?       if ld_work_start_date > off_work_date         self.errors.add(:base, "work start date can't greater off work date") , return false       end     elsif ld_work_start_date.present? && !off_work_date.present?       self.errors.add(:base, "off work date can't blanck") , return false     elsif off_work_date.present? && !ld_work_start_date.present?       self.errors.add(:base, "work start date cant't clanck") , return false      end    end 

you can use rails presence validator start , end date.

validates :ld_work_start_date, presence: true validates :off_work_date, presence: true 

then use own validation method check start date before end date.

validate :start_date_before_end_date, if: proc.new{|x| x.ld_work_start_date.present? && off_work_date.present? }  def start_date_before_end_date   if ld_work_start_date > off_work_date     errors.add(:base, "work start date can't greater off work date")   end end 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -