<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: foreign code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 03:23:49 GMT</pubDate>
    <description>DZone Snippets: foreign code</description>
    <item>
      <title>Simulating a foreign key constraint in ActiveRecord (Ruby on Rails)</title>
      <link>http://snippets.dzone.com/posts/show/3400</link>
      <description>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. &lt;br /&gt;&lt;br /&gt;It wasn't entirely obvious (to me) how to achieve this in an ActiveRecord model - you need *both* of the following two lines:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  validates_presence_of :category, :message =&gt; " must be specified" &lt;br /&gt;  validates_associated :category&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Hope this helps someone. </description>
      <pubDate>Thu, 01 Feb 2007 00:55:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3400</guid>
      <author>eddiewould (Eddie)</author>
    </item>
  </channel>
</rss>
