rake freeze_gems fails because rails gem is not installed

that the code is not picking up the right version for rails.

A quick look at:

gem list

receals that I have:

rails (1.0.0)
    Web-application framework with template engine, control-flow layer, and ORM.

rails_analyzer_tools (1.1.0)
    Tools for analyzing the performance of web sites.

And 1.1.0 is exactly what it shows as my version of rails. Well, again, I beg to differ!

The quick and dirty fix:

edit /usr/local/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/tasks/framework.rake

and hardcode the version:

rails = if version = ENV[‘VERSION’]
        Gem.cache.search(‘rails’, “= #{version}”).first
      else
        Gem.cache.search(‘rails’).sort_by { |g| g.version }.last
      end
version ||= rails.version
version  = ‘1.0’  #HARDCODE VERSION

Now, freezing rails works as expected:

rake freeze_gems

An other solution may be what Michael Gorsuch did.

Freeze all your ruby gems on a shared host

Things started to go downlhill when I had to check back on a previous article I had written about (Allowing full access to a sub directory in Apache Server) and my server would just hang!
Well, not a problem, let’s just try to hit the refresh key, hmmm, nothing. Ok, for good measure, let’s hit the button, just to be sure. Hmmm, still nothing.
Ok, let’s check out the logs, started from the apache logs.

[Tue Mar 28 21:04:31 2006] [error] [client 66.249.66.226] FastCGI: incomplete headers
(0 bytes) received from server “/home/psq/blog.nanorails.com/current/public/dispatch.fcgi”

Ok, yep, something’s wrong. Next, let’s hit the production.log. Nothing useful.

Next, a trick I learned before (aren’t you glad it is not your first time troubleshooting FastCGI?). Let’s call public/dispatch.fcgi directly:

/usr/local/lib/site_ruby/1.8/rubygems.rb:194:in `report_activate_error’:
    RubyGem version error: activerecord(1.13.2 not = 1.14.0) (Gem::LoadError)
    from /usr/local/lib/site_ruby/1.8/rubygems.rb:136:in `activate’
    from /usr/local/lib/site_ruby/1.8/rubygems.rb:162:in `activate’
    from /usr/local/lib/site_ruby/1.8/rubygems.rb:161:in `each’
    from /usr/local/lib/site_ruby/1.8/rubygems.rb:161:in `activate’
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:23:in `require’
    from ./../config/boot.rb:14
    from ./../config/environment.rb:8:in `require’
    from ./../config/environment.rb:8
    from ./dispatch.fcgi:21:in `require’
    from ./dispatch.fcgi:21

Ah ha! Bingo. What does it all mean, though?

It has been hard to miss that Rails 1.1 is not officially out. Yeahh!!! I wonder whether Dreamhost (aff) has maybe, just maybe, decided to upgrade.

gem list|grep rails

Ah ha! again. Yes, rails is is now 1.0 and 1.1. So I futz a little bit to try to freeze the gems manually, to not avail :(. So I go home, and fortunately, thanks to Capistrano, I have a full copy on my home machine! After doing a little reading, I figure the solution has to be:

rake freeze_gems

So first things first, I get home, rush to my machine, run the magic command, then run what was going to solve all the recent woes:

cap deploy -q

and hit the refresh button…
Duh! Still doesn’t work. Major Bummer!

Next step is to get a copy of all my gems (for typo):

 cd vendor/rails
 gem unpack activerecord
 gem unpack activesupport
 gem unpack actionpack
 gem unpack actionmailer
 gem unpack actionwebservice
 mv activerecord-1.13.2/ rails/
 mv actionmailer-1.1.5/ actionmailer
 mv actionpack-1.11.2/ actionpack
 mv actionwebservice-1.0.0/ actionwebservice
 mv activerecord-1.13.2/ activerecord
 mv activesupport-1.2.5/ activesupport

Your versions may vary depending on your install.

Then, committed everything, and redeployed.

and Ta Da! everything’s now back to normal! Phew…

The moral of the story, it that wasn’t clear already is:

Do not rely on the gems installed on your shared host!
Make sure your freeze all your gems

And I can worry on my own time to upgrade to rails 1.1 in my production environmment.

Update about 2 hours later:
Assuming you can run rake (which I couldn’t because of some broken dependency), an other method to lock down to rails 1.0 is to use:
rake freeze_edgeREVISION=3303

this assumes that’s you are using rails directly from svn already.

Update about 18h later:
An other solution is to lock down the versions of the gems you use in environment.rb using what suggested on Ruby On Rails Wiki. Thank you, Miles Barr for the tip!

Debugging in Rails

In my own experience, here’s what a good debugger needs:

  1. breakpoints (with conditions even better)
  2. stepping
  3. object list (locals, globals, objects)
  4. watches
  5. resetting the current instructions
  6. modify objects

4, 5 or 6 are really icing on the cake, but help separate the men from the boys. Although these can help save a tremendous amount of time, especially when a certain situation is really hard to reproduce because you get several shot at reproducing and fixing the problem in real time.

Best examples I have used (and abused): Steve Jasick’s “The Debugger”, and yes, Visual C++ (before the .net and C# days). Eclipse and the java debugger is not too bad either.

So why is this such an unpopular subject with such a limited support with Ruby and with Rails? Enquiring mind wants to know!

Best usable option in practice: breakpoint.

It meets requirements 1 through 3. Hey, you can even do conditional breakpoints!

Just insert a “breakpoint” call where you want to stop, and then run you app normally.

While you do that, run script/breakpointer to catch the breakpoint (assumes you are on the same machine).

There, you can check out any ruby expression like you can do in irb.

Some useful commands to help find out what’s there:

  • local_variables (list of all your local variables (whether initialized yet or no)
  • instance_variables (same as self, without the values)
  • caller (shows the stack trace, not terribly readable but the information is there)
  • methods (list all the method of the class you stopped in)

Just need to tough it up and get on with it till someone comes up with a better alternative. This brings back some fond memories of long ago when command line debuggers where all the rage, and all you had too. But I’m starting to date myself here so I’ll just shut up and set a few breakpoints to relax.

A word to the wise: do me a favor, and remove your breakpoints before deploying on your production environment. This could save you from ridicule. Not that this ever happened to me, right!

I’m very hopeful that the guys are RadRails will find a solution to get it to work faster and more reliably.

RadRails 0.6.1 released

a few bug fixes and a few improvemens. Especially switching between view and controller now suppors RHTML, RXML and RJS.

If you’ve created test scripts with 0.6, be sure to delete the scripts and regenerate them. See details on radais.org

Download 0.6.1

I recommand you use the site update as I explained in the 0.6 announcement. This greatly simplifies updates.

Adding the Ruby builder to your RadRails project

To add the ruby validator to your project, open your .project file (at the root of your project).

And add:

<buildCommand>
    <name>org.rubypeople.rdt.core.rubybuilder</name>
    <arguments>
    </arguments>
</buildCommand>

inside the

element.

Refresh eclipse, and wait for it to rebuild everything. Then your files will be checked for correct ruby syntax everytime you save. You will see a little marker everytime there is an error.

Here’s the complete .project file:

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>typo</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
            <buildCommand>
                    <name>org.rubypeople.rdt.core.rubybuilder</name>
                    <arguments>
                    </arguments>
            </buildCommand>
    </buildSpec>
    <natures>
            <nature>org.radrails.rails.ui.railsnature</nature>
    </natures>
</projectDescription>

Switching the nature of a project to RadRails

Open your .project file, and replace the element with

<natures>
    <nature>org.radrails.rails.ui.railsnature</nature>
</natures>

Here’s how my project looks like

<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>typo</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
    </buildSpec>
    <natures>
        <nature>org.radrails.rails.ui.railsnature</nature>
    </natures>
</projectDescription>

Then either refresh your project or restart eclipse/RadRails. The Project icons should now have the little decorator with the Rails logo.

Best practices

glu.ttono.us has posted an excellent summary of his take on Rails best practices.

And I think I need to do some reading, that the second time in as many days that someone mentions Refactoring by Martin Fowler, and this has me intrigued.

My top favorites:

  • testing of course
  • I second Luke. Selenium and Selenium on Rails is a better approach for functional (and user type testing). I’m not saying this should replace it. Use both techniques.
  • DRY (Don’t Repeat Yourself)