1

I have a sqlite3 database with 3 tables which has id column as primary key, but has no created_at or update_at columns.

I want to use it in a Rails3 Applications. How can I properly convert it into a 'Rails database'?

Daniel Rikowski
  • 71,375
  • 57
  • 251
  • 329
aztack
  • 4,376
  • 5
  • 32
  • 50

2 Answers2

1

It sounds like you want a single rails app to access two different databases? You will probably need to do two things:

  • You will need to have your app connect to two different databases. Your "real" database and the sqlite3 db
  • After you can successfully connect to both DBs, you MAY need to setup models and override some of the default Rails naming conventions.

For the first item, you can follow this: Connecting Rails 3.1 with Multiple Databases

For the second item, if the tables don't follow the Railsy way of naming them, you can could create a model that looks something like this:

# app/models/foo.rb
class Foo < ActiveRecord::Base
  establish_connection "your_sqlite_connection_name_#{Rails.env}"
  self.table_name = "name_of_table_in_sqlite_db"
end
Community
  • 1
  • 1
Mario Zigliotto
  • 8,315
  • 7
  • 52
  • 71
0

Write a migration to add those two fields the table(s) in question.

For instance,

Command line something like script/generate migration add_timestamps_to_user

Then edit the migration and put the two fields in there.

e.g.

def change
  add_column :users, :created_at, :date
  add_column :users, :updated_at, :date
end

Also, make sure that foreign keys are in the format othertablename_id and if not, consider renaming them with a rename_column migration.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • Thanks. Is there a quick way to copy exits data into rails database. I know how to write a script to 'manually' do that. But i wonder if there a better way? – aztack May 13 '13 at 00:27
  • When you say exits do you mean existing? The database will remain the exact same sqlite3 database even when the access is from rails. – Michael Durrant May 13 '13 at 00:44