Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Simulating a foreign key constraint in ActiveRecord (Ruby on Rails) (See related posts)

Say for example we have products and a business rule that every product must have a category. Before ActiveRecord we would have done this with a NOT NULL foreign key constraint at the database level.

It wasn't entirely obvious (to me) how to achieve this in an ActiveRecord model - you need *both* of the following two lines:

  validates_presence_of :category, :message => " must be specified" 
  validates_associated :category


Hope this helps someone.

You need to create an account or log in to post comments to this site.


Click here to browse all 4863 code snippets

Related Posts