Simulating A Foreign Key Constraint In ActiveRecord (Ruby On Rails)
Join the DZone community and get the full member experience.
Join For FreeSay 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.
Relational database
Opinions expressed by DZone contributors are their own.
Comments