Reputation:
I'm trying to use ActiveRecord and rails migration in a non-rails project. I installed the standalone_migrations gem and it seems to work. To test, I created two migration with
rake db:new_migration name=users
rake db:new_migration name=logins
Here are my migration files:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.string :full_name, :null => false
# Database authenticable
t.string :username, :null => false, :default => '', :unique => true
t.string :encrypted_password, :null => false, :default => ''
end
end
def self.down
drop_table :users
end
end
class CreateLogins < ActiveRecord::Migration
def self.up
create_table :logins do |t|
t.integer :user_id, :null => false
t.datetime :logged_in_at, :null => false
t.datetime :logged_out_at, :null => false
end
end
def self.down
drop_table :logins
end
end
I then ran rake db:migrate
and it seems to work:
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0020s
== CreateUsers: migrated (0.0020s) ===========================================
== CreateLogins: migrating ===================================================
-- create_table(:logins)
-> 0.0020s
== CreateLogins: migrated (0.0020s) ==========================================
I have a db/migrate
folder and a db/config.yml
configuration just as in the standalone_migrations gem manual. However, when I create a user model:
require '../../db/dbconnect'
p ActiveRecord::Base.connection.tables
class User < ActiveRecord::Base
end
User.new
First I get an empty list for the tables and then an exception
Could not find table 'users' (ActiveRecord::StatementInvalid)
What can I do to make it work?
EDIT 1:
My db/config.yml
:
development:
adapter: sqlite3
database: development.db
encoding: utf8
production:
adapter: sqlite3
database: aquareader.db
encoding: utf8
test: &test
adapter: sqlite3
database: test.db
encoding: utf8
And my db/dbconnect.rb
:
require 'rubygems'
require 'active_record'
require 'yaml'
# read the connection information from the database config file
dbconfig = YAML::load(File.open(File.expand_path(File.dirname(__FILE__) + '/config.yml')))
# connect to the database
ActiveRecord::Base.establish_connection(dbconfig['development'])
EDIT 2:
I found that my developement.db
table was being create my the migrations in my root folder with the correct tables and everything. However, when I run lib/myproject/users.rb
it creates a development.db
in the project directory and doesn't use the one specified in the dbconnect.rb
file. Any ideas why?
Upvotes: 2
Views: 1288
Reputation:
The solution to my problem was so obvious that I'm quite ashamed I asked a question here.. All I had to do is set the absolute path to the database file in my config.yml
.
Upvotes: 1
Reputation: 7156
Hmm.. I suspect something with the naming might be a problem. Rails uses 'magic' to derive the table name from an ActiveRecord class.
Can you show how your tables are actually named in your database with: describe tables
or the equivalent for your data store?
Upvotes: 0