Ruby on Rails Monday, February 28, 2011

One option would be to put it in the controller. You can simply check the dates and create an instance variable containing an error message. That isn't very DRY though. Want to do it somewhere else? Then you're duplicating the code.

Another option is to create a dummy model that does not inherit from ActiveRecord, then include the validations there. If you add the right methods, you can interact with it just like a normal AR model. Something like this should get you started:

class MySearchClass

  include ActiveModel::Validations

  validates :start_date
  validates :end_date

  def save

    return false unless valid?
    # the dates were valid, go ahead and query the records
    return true
  end

end

--
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.

No comments:

Post a Comment