Very often I have to work on databases which do not follow ActiveRecord convention and making ActiveRecord models, if number of tables is large, is very slow and boring task. In order to speed up and simplify it I’ve created Rmre gem. Gem is quite simple yet you might find it useful if you want to create fixtures, migrations or simply port application to Ruby on Rails.
So how it works? For each table in the database, gem creates model. Name of the model is created using Rails `classify` method. Moreover, if table’s primary key is not column named “id” gem sets primary key by adding `setprimarykey “primaryKeyColumnName”` line to the model. In addition for MySQL, PostgreSQL, Oracle or MS SQL foreign keys are analyzed and for each constraint gem generates `belongsto` or `hasmany` lines. Here is model created for table store in Sakila MySQL test database:
class Store < ActiveRecord::Base set_primary_key :store_id set_table_name 'store' has_many :customers, :class_name => 'Customer' has_many :inventories, :class_name => 'Inventory' has_many :staffs, :class_name => 'Staff' belongs_to :address, :class_name => 'Addres', :foreign_key => :address_id belongs_to :staff, :class_name => 'Staff', :foreign_key => :manager_staff_id end