Use Rails logger in tests October 5, 2007

Posted by Slobodan Kovacevic in : Resources and Links, Ruby & Rails , add a comment

For some debugging in Rails test we wanted to be able to log data via Rails logger. Unfortunately, as it turns out you cannot directly access logger from tests (which is strange). Since we needed to add information to test.log we used this small piece of code (added to test_helper.rb):

def logger
RAILS_DEFAULT_LOGGER
end

and just use it in tests like this:

logger.info(’performing test now’)

[via Robby on Rails]

Nested :include ActiveRecord option June 23, 2007

Posted by Slobodan Kovacevic in : Programming, Ruby & Rails , add a comment

While working on a site I had User, Order, OrderItems and Product models and they were related like this:

Order belongs_to User
Order has_many OrderItems
OrderItem belongs_to Product

So, when displaying an order I found myself doing accessing product through order_item, i.e. @order.order_items.product.name.

Although this worked problem was that even though I have told Rails using :include to eager load order_items it didn’t load products, so whenever I accessed a product it executed a separate SQL query. In other words, to display order with 10 products it required 11 SQL queries - hardly optimized.

Rails API documentation doesn’t say anything about eager loading “second” level, but after some searching I found that you can do that you can do nested :includes, like this:

Order.find(id, :include => [{:order_items, :product}, :user]

This will eager load User and OrderItems, but it will also eager load Products under each OrderItem. This decreased my number of queries from 11 for 10 product order to only one (big one).

You can read more (for example, it appears that you can nest multiple :includes) about this undocumented feature in ‘The beauty of a nested ‘:include’ option’.

LocalizationSimplified April 26, 2007

Posted by Slobodan Kovacevic in : Ruby & Rails , 4 comments

Rails is excellent in handling almost everything if you are building a site in English. Unfortunately, when it comes to localization (sites in other languages, UTF-8 support, etc.) Rails is not very well equipped to handle it.

There are localization plugins that make life easier, but most of them are rather complex, hard to use and have significant overhead (i.e. takes time to setup, learn, etc.). Fortunately, plugin called Localization Simplified aims to simplify localization as much as possible:

Fast and easy localization of one-language applications. Adds UTF-8 support for Ruby + database. Modifies ActiveRecord errors + html error helpers, Date/Time helpers, locale time formats, to_currency, to_sentence

It’s easy to use mainly because it only overrides existing Rails methods, so you don’t have to do anything special to use it.

Localization Simplified is a great plugin which you can download from Ruby Forge.

Rails Source Code Annotations March 4, 2007

Posted by Slobodan Kovacevic in : Ruby & Rails , 1 comment so far

Ruby On Rails
I’ve been catching up on my blog reading and I’ve stumbled upon an article describing a great new feature in Edge Rails that lets you add annotations to your code. You can use TODO, OPTIMIZE and FIXME to mark parts of your code. Then you can use rake notes to list annotations - which is great since you won’t miss any of the notes you made.

The only problem is that this is still in only in Edge Rails, i.e. it’s not yet in stable Rails release. But there’s a really simple way to add source code annotations rake task to non-Edge Rails app:

svn export http://svn.rubyonrails.org/rails/trunk/railties/lib/tasks/annotations.rake \
 lib/tasks/annotations.rake

For more information about Rails source code annotations see Ryan’s blog.

Rails Simplified: Migrations December 25, 2006

Posted by Slobodan Kovacevic in : Programming, Ruby & Rails , add a comment

When you start learning Ruby and Ruby on Rails you are faced with very powerful set of tools and sometimes it can be overwhelming. You are often faced with long cryptic explanation of some feature and all you want is a quick overview of what it does and how you could use it.

Rails Simplified will be a series of posts is giving you a brief description of various Ruby / Rails related features, as well as pointer to other articles that give you a more in-depth view of those features. Rails Simplified - Migrations will be the first one in this series…

Rails Migrations

What are migrations?

Migrations are an easy way to manage your database schema in a way that you can easily synchronize it between different servers (for example between development and production server). Simply put: you can write code that can upgrade or downgrade your database schema - think of it as database versioning.

If you did any programming you probably know how hard it can be to keep track of database changes - migrations make your life a lot easier.

How do you use migrations?

  • Create migration using: ./script/generate migration ShortDescriptionChange
  • Edit file that was created in db/migrate dir and describe changes that need to be made
  • Run rake migrate which brings your database up to date

How do you describe schema changes?

Once you create migration defined class will have two methods self.up (used when upgrading schema) and self.down (downgrade schema). If you want to create a table you’d write code like this:

  def self.up
    create_table :lists do |t|
      t.column :name, :string
      t.column :description, :string
      t.column :created_on, :datetime
    end
  end

  def self.down
    drop_table :lists
  end

First method defines table that needs to be created (notice that it’s database independent) and second provides a way to undo changes made by this migration.

These are just a simple example and you can do a lot more using migrations - change existing tables, delete tables, insert default values to tables and if necessary you can even execute plain SQL code.

Where can I get more information about Rails migrations?

Amazon on Rails - search Amazon using Rails and Ruby/Amazon August 31, 2006

Posted by Slobodan Kovacevic in : Programming, Ruby & Rails , 4 comments

Currently I am working on my first big Rails application and I needed an simple and quick way to get matching movies and music based on user entered keywords. The best place to get such information is Amazon as they have almost anything in their store.

So I decided to write a small Rails application that takes input from user and displays products from Amazon matching that criteria - in essence Amazon search. In the end it will look something like this - Amazon on Rails
(more…)

Install Ruby and Ruby On Rails on Ubuntu July 2, 2006

Posted by Slobodan Kovacevic in : Ruby & Rails , 6 comments

Recently I started messing around with Ruby and I wanted to set it up on our local server, so I gave InstantRails a go - and it worked perfectly.

Meanwhile we changed our operating system and switched from Windows to Linux (Ubuntu Dapper), so now I needed Rails for Ubuntu. Unfortunately (or luckily) there is no InstantRails for Ubuntu. The only way to go was to install “real” Ruby and Rails, so I had to figure out how to install it once again.

Again I tried to find a meaningful and complete tutorial, but I didn’t found it so I had to glue pieces from different sources (you have a complete list of articles I used at the end). I wanted to do this as simple as possible and without any compiling.
(more…)

Ruby installation on Windows June 10, 2006

Posted by Slobodan Kovacevic in : Programming, Ruby & Rails , 3 comments

Recently I’ve started playing around with Ruby and Ruby On Rails on our hosted server. Since I liked it a lot I have finally decided to try to install it on our local Windows server. While I was doing it I found that most of the online tutorials are not all completely accurate, i.e. they need a bit of tweaking… So I started logging every step I did.
(more…)