rubycoloredglasses


I'm Jason, a web application developer in East Tennessee.


  1. Ruby on Rails session - Access from PHP

    If you need to access a Ruby on Rails session from a PHP application running under the same domain, you can do this by splitting the string in the cookie by the ‘–’. Thanks to Frederick Cheung for pointing this out.

    Here is coding I added to my PHP script which was running from a path under the same domain. Unfortunately the data returned is in Marshal format, and there isn’t a Marshal.load function for PHP to get the values easily.

    $raw_session_string = $_COOKIE['_app_session'];
    $data_split = explode ('--' , $raw_session_string);
    $encoded_session = $data_split[0];
    $marshal_data = base64_decode($encoded_session);
    echo "<pre>marshal_data:". print_r($marshal_data,1) ."</pre>\n";
    

  2. Obtaining Request Domain Name for Ruby on Rails

    I’m using Rails 2.3.8. To obtain the domain name for the website being requested (i.e. mysite.com, mysite.net), just reference ‘request.host’.

    ruby@host = request.host
    

    You can only reference request.host in the views, or the controller.


  3. Changing Column Order via ActiveRecord Migration

    Is it possible to change the order of the columns in your MySQL (or other database) table using a migration? Lets see.

    If you check the ActiveRecord::Migration documentation you’ll see there is a method called ‘change_column’ which accepts various options.

    <tt>change_column(table_name, column_name, type, options)</tt>:
    Changes the column to a different type using the same parameters as add_column
    

    As of Rails 2.3.6 this is now available by using the :after option. You’ll have to include the field type, even though you are not modifying the type.

    Example:

    change_column :orders, :tax_rate, :float, :after => :tax_state
    

  4. Rails Performance Statistics

    Again, as I search for things, I stumble onto new tools. I just found out about a tool for monitoring the performance of Java and Ruby applications called New Relic.

    They provide a free service level for Startups and Students even.


  5. RailRoad Gem

    I just discovered that there is a Ruby gem which generates diagrams based on Rails models (ActiveRecord). I ran across this website a while back, but didn’t quite connect the dots. I was just reading an article on placing models into their own namespace, and I realized that the diagram it uses as an example was generated using RailRoad.

    [http://railroad.rubyforge.org/]

    Railroad model diagram


  6. Annotate Models

    There is a rails plugin which adds schema information for the models in comments at the top of your model definition files. It’s really useful. Check out the instructions on installing and using this plugin at:

    [http://pragdave.pragprog.com/pragdave/2006/02/annotate_models.html]


  7. Selenium RC, Firefox 3, and Ubuntu

    Selenium Logo

    I’ve got a system setup which uses Firefox on an Ubuntu machine, with the Selenium RC server (remote control). I had a set of scripts which would run automatically every 15 minutes, which would prompt Firefox to open and go to the site and submit certain forms. This stopped working after I ran an update on some packages in my Ubuntu machine (9.04 Jaunty).

    I was able to resolve this issue by upgrading from Selenium RC 1.0.1 to 1.0.3.


  8. Undefined method 'ref' for ActiveSupport::Dependencies:Module

    After upgrading to Snow Leopard, and trying to run ‘rake db:migrate’, I received this error once. This seems common to others which have upgraded, especially back when Snow Leopard was released in August of 2009:

    rake aborted!
    uninitialized constant MysqlCompat::MysqlRes
    (See full trace by running task with --trace)
    

  9. Setting up Deployment for Rails using Capistrano, Apache with Passenger and Git

    I don’t have time right now to learn how to setup Capistrano. I just want a recipe that works and does the job. Here are my notes.

    1. First install the Capistrano gem
    sudo gem install capistrano
    
    1. Next you need to go into the directory of your Ruby on Rails application and capify it:
    capify .
    

  10. Rake Tasks

    If you’re wanting to know which Rake tasks are available for you to use from the command line, simply use the ‘rake -T’ command:

    $ rake -T
    (in /Users/jason/railsproject)
    rake db:abort_if_pending_migrations       # Raises an error if there are pending migrations
    rake db:charset                           # Retrieves the charset for the current environment's database
    rake db:collation                         # Retrieves the collation for the current environment's database
    rake db:create                            # Create the database defined in config/database.yml for the current RAILS_ENV
    rake db:create:all                        # Create all the local databases defined in config/database.yml
    rake db:drop                              # Drops the database for the current RAILS_ENV
    rake db:drop:all                          # Drops all the local databases defined in config/database.yml
    

    A really useful one is the ‘routes’ option which outputs a list of the routes configured.

    macbook:railsproject jason$ rake routes
    (in /Users/jason/railsproject)
      /:controller/:action/:id
      /:controller/:action/:id(.:format)