<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>David Chelimsky: don't let :name_prefix result in the name of an existing model</title>
    <link>http://blog.davidchelimsky.net/articles/2007/05/01/dont-let-name_prefix-result-in-the-name-of-an-existing-model</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>on software in process and practice</description>
    <item>
      <title>don't let :name_prefix result in the name of an existing model</title>
      <description>&lt;p&gt;Here&amp;#8217;s something that bit me today. I&amp;#8217;ll bet it&amp;#8217;s documented somewhere, and it makes perfect sense, but maybe I can help you avoid this in case you missed the docs like I did.&lt;/p&gt;


	&lt;h3&gt;The situation&lt;/h3&gt;


	&lt;p&gt;I&amp;#8217;m working on an asset management system which includes Categories and Tags, which have a many-to-many relationship expressed by a CategorizedTag model.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;class Category &amp;lt; ActiveRecord::Base
  has_many :tags, :through =&amp;gt; :categorized_tags
  has_many :categorized_tags
end

class Tag &amp;lt; ActiveRecord::Base
  has_many :categories, :through =&amp;gt; :categorized_tags
  has_many :categorized_tags
end

class CategorizedTag &amp;lt; ActiveRecord::Base
  belongs_to :category
  belongs_to :tag
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;The problem&lt;/h3&gt;


	&lt;p&gt;In routes.rb, I wanted to nest tags inside categories:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;  map.resources :categories do |categories|
    categories.resources :tags, :name_prefix =&amp;gt; 'categorized_'
  end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This seemed fine, but I got a nil-pointer error on categorized_tags_path in a view.&lt;/p&gt;


	&lt;h3&gt;The fix&lt;/h3&gt;


	&lt;p&gt;Guessing that there was a naming conflict with CategorizedTag, I tried this instead (&amp;#8216;category_&amp;#8217; instead of &amp;#8216;categorized_&amp;#8217;):&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;  map.resources :categories do |categories|
    categories.resources :tags, :name_prefix =&amp;gt; 'category_'
  end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Sure enough, category_tags_path worked just fine!&lt;/p&gt;


	&lt;h3&gt;The moral&lt;/h3&gt;


	&lt;p&gt;So make sure that when you use :name_prefix that it doesn&amp;#8217;t result in the name of an existing model.&lt;/p&gt;</description>
      <pubDate>Tue, 01 May 2007 19:24:18 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:c59f37f0-5a8f-49d2-99d9-9e5e4fdee0b7</guid>
      <author>David</author>
      <link>http://blog.davidchelimsky.net/articles/2007/05/01/dont-let-name_prefix-result-in-the-name-of-an-existing-model</link>
      <category>rails</category>
      <category>nested_resources</category>
    </item>
  </channel>
</rss>
