Model validation with Rails

Ruby On Rails Rails has very nice features to validate a model (directly mapped on your database). However it gets sometimes sloppy if you don’t know the exact command to do your validation.Here’s a trick I had a hard time to find.

Everybody knows how to validate the uniqueness of a field in a database :

validates_uniqueness_of :date

If you now want to validate the uniqueness of a couple (ex: date,user_id) here’s how you can:

validates_uniqueness_of :date, :scope => :user_id
This entry was posted in Ruby on Rails and tagged , , . Bookmark the permalink.

2 Responses to Model validation with Rails

  1. if anyone is wondering about how to get the error message to show in the view here it is:

    I spent some time trying to find the answer to this one

  2. i had to take out the surrounding symbols because they didnt show, maybe the author can fix this

    = error_messages_for ‘model_name_goes_here’

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>