This weekend I thought I would have a go at throwing together a Rails front end to my mythtv database. I thought this would be easy but it seemed to highlight one difficulty after another with using Rails with a so called “legacy” application (one that already exists and not designed for Rails).
To start with I used the Rails install that comes with OS X. I did something like
rails mythrails
cd mythrails
#edit database.yml
ruby script/generate scaffold Program program
Since this is a so called legacy application you need to edit config environment.rb to tell rails not to pluralize table names
ActiveRecord::Base.pluralize_table_names = false
and edit the model to tell it what the table is actually called
class Program < ActiveRecord::Base
set_table_name "capturecard"
end
then starting the web server gives you a web interface to the program table. It’s really simple. But …
If you try to edit the data or create a new entry it will complain because (I think) there is no id field in the program table. This lead me to discover that you can add something like
class Program < ActiveRecord::Base
set_table_name "capturecard"
set_primary_key "the_id_field"
end
This raises the next problem. Rails can not cope with composite primary keys. You need to install composite_primary_keys. However, this leads to the next issue - composite_primary_keys needs Rails 2. This then has the side effect that generating the scaffold no longer works. In Rails 2 you need to specify the fields and types for all the fields in the table. In the case of a legacy system the table already exists so why can’t the scaffold generator just read the fields from the database?
At this point I gave up. I am sure this is not too hard to work around but I only wanted to play and this all seemed like it was getting harder than I ever intended.