http://podloni.co.cc Rebecca’s Ramblings » Rails

Archive for the ‘Rails’ Category

“Time” columns and Rails

Thursday, January 3rd, 2008

In SQL, there is a data type TIME which holds time of day information without date information. There is no equivalent class in Ruby. Rails (ActiveRecord) deals with this by adding some dummy date data and using the standard Ruby Time class. Currently the dummy date used is 2000-01-01, though since I can’t find any documentation on this, I guess it could be changed to something else in the future.

This means you can run into some odd issues if you aren’t careful when comparing values from SQL TIME columns with times in your code.

For example, if you have a record r in your database with the value “13:30:00″ stored in a column called arrival_time of SQL data type TIME:

Time.parse("2000-01-01 13:30") == r.arrival_time       # unless the dummy date AR uses has changed

"13:30:00" == r.arrival_time_before_type_cast

Time.parse("13:30") != r.arrival_time         # unless today is 1st January 2000

More on date_select/select_date

Wednesday, October 3rd, 2007

I’ve recently been attempting to write Javascript code to work with the values in select boxes created through both date_select (works with dates that are accessed through a method on an object) and select_date (works with any date object you pass to it). Rather inconveniently, Rails names/ids the select boxes differently for these 2 cases.

date_select style ids:
id="<objectname>_<methodname>_3i" for day
id="<objectname>_<methodname>_2i" for month
id="<objectname>_<methodname>_1i" for year

select_date style ids:
id="<prefix>_day" for day
id="<prefix>_month" for month
id="<prefix>_year" for year
where <prefix> is either “date” or whatever you specify in the options to select_date.

What happens when a migration fails

Friday, August 3rd, 2007

So… you’re writing a migration, you think you’ve got it right - time to try it out. You run
rake db:migrate
to apply your migration, and it doesn’t work.

Your database has some of the changes from the migration in it, and the rest aren’t there. How do you get it back into the state it was in before you attempted the migration?

You might think that
rake db:migrate VERSION=nn
(where nn is the migration number before the one that failed)
would do the job, presuming that your failed migration’s self.down method runs the statements to rollback your changes in the same order as the self.up method makes the changes.

Unfortunately, nothing happens. Rails knows it didn’t complete the migration, so has left the current schema version number as at the end of the last migration which did complete.

At this point, you are faced with opening up your database and reverting the changes that did get made manually…

There is, however, a patch in rails-trac to fix this issue - if you are using a database that supports transactions round data definition statements (such as Postgres). Once installed, your migration is wrapped in a transaction, which is rolled back if any errors occur. Yippee!

How to get references, functions etc into your Rails test database

Monday, June 25th, 2007

Your rails app comes with 2 tasks to clone your database structure into the test database: db:test:clone, which does everything through ruby, and db:test:clone_structure, which dumps the SQL needed to recreate the database structure, then reloads it, in a DB-specific fashion. You can switch which version is run by the db:test:prepare task in environment.rb.

Find this bit:

# Use Active Record's schema dumper instead of SQL when creating the test database
# (enables use of different database adapters for development and test environments)
# config.active_record.schema_format = :ruby

Uncomment the last line shown above, and replace :ruby with :sql - that’s it!

If, like me, you’re using postgres databases, this does the trick - now db:test:prepare dumps the structure of your dev/production database using pg_dump -s … and loads it into the test database using psql.

A couple of caveats:

  • If you’re using functions written in a language that needs to be created after a new database is created, you must connect to your test database as a superuser, as only superusers can add languages.
  • If you’re using views in MySQL, the views are carefully removed from the dumped structure, so won’t be in your test database.

Running rake in production mode

Monday, June 18th, 2007

This is the second time now that this has had me tearing my hair out. Last time, I couldn’t work out how to get rake db:migrate to operate on the production database. After a while on Google, I managed to find:

%> rake --require=config/environment.rb db:migrate

It worked like a treat, even on the Windows server my app’s deployed on (though with \ as the path separator, not /).

Until now, that is.

I’ve just switched to using the paginating_find plugin for paging result sets, as it allows me to use all the find options, unlike the rails default pagination. It works fine in both development and production, and rake is happy in development mode. It doesn’t like the command above, though, if you happen to call the find method in your migration. For some bizarre reason, the find method aliasing goes wrong, and you get an infinite loop. The overwritten find method is supposed to call the original find method, but ends up calling itself recursively instead.

Back to Google… nothing on this problem that I could find.

Rethink: it must be something with the way production mode is specified, since it’s only a problem when running rake like this.

Back to Google again: this time to look for alternative methods to specify that rake should use the production environment. Finally found a way, thanks to a post on a TextDrive forum:

%> RAILS_ENV=production rake db:migrate

That solves the problem on Linux, how about on Windows?

%> set RAILS_ENV=production
%> rake db:migrate

Sorted!

Real World Rails

Friday, May 18th, 2007

IBM developerWorks has started a series called Real World Rails. The first article covers how to scale a Rails app using caching. There are some good reference links too.

Caching in Rails