<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/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>I.NFECTIO.US: RAILS_ROOT and Dir.chdir</title>
    <link>http://i.nfectio.us/articles/2007/02/10/rails_root-and-dir-chdir</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>"Nothing in life is worth doing if you have no life while doing it"</description>
    <item>
      <title>RAILS_ROOT and Dir.chdir</title>
      <description>&lt;p&gt;It's been awhile since I've blogged on Rails, got diverted to a Novell to Windows conversion over Christmas break. A local community college requested emergency assistance with completing their conversion and asked us to do it in 2 weeks. My team and I got it done but it was a hell of a lot of work. Now that I'm back to programming, I ran into a little annoyance last night that took awhile to troubleshoot down. I'm working on a little app to provide a web interface to my book collection. My book repository is located elsewhere on my harddrive so I used Dir.chdir to move to the root of the repository.&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;
To make a long story short, DO NOT use Dir.chdir in your Rails Applications. RAILS_ROOT, which is used to locate dependencies is a relative path. If you change the working directory of your Rails application, Rails may/will have problems loading dependencies. In my case, I used Dir.chdir to move to then iterate the publisher directories and add books found onto the publisher.books has_many association. This promptly failed with Publisher::Book uninitialized constant errors. The dependency loading mechanism uses RAILS_ROOT to find items in your load_paths and being a relative path, changes if you change the current working directory.&lt;/p&gt;
&lt;br /&gt;
&lt;p&gt;I believe one solution, which I didn't test, would be to set RAILS_ROOT to an absolute path in your environment.rb. The solution I chose was to not chdir but use a full path in each Directory or File operation i used. And now you know.&lt;/p&gt;</description>
      <pubDate>Sat, 10 Feb 2007 13:29:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:bf184585-13b0-4f89-8024-c05fc49cf1e8</guid>
      <author>Bob Silva</author>
      <link>http://i.nfectio.us/articles/2007/02/10/rails_root-and-dir-chdir</link>
      <category>Ruby on Rails</category>
      <category>ruby</category>
      <category>rails</category>
      <category>chdir</category>
    </item>
    <item>
      <title>"RAILS_ROOT and Dir.chdir" by jteam</title>
      <description>I had the same problem - I was building a Ferret search index in a controller (I believe Ferret is doing Dir.chdir at some point) and afterwards ror couldn't compile/resolve the *.rhtml files anymore...
My solution was to set the RAILS_ROOT in environment.rb. It does the job for now.</description>
      <pubDate>Fri, 02 Mar 2007 02:18:45 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:4649da89-0587-4865-ae65-1383ed1a4d73</guid>
      <link>http://i.nfectio.us/articles/2007/02/10/rails_root-and-dir-chdir#comment-267</link>
    </item>
  </channel>
</rss>
