[Ruby] Using ActiveRecord outside Rails
Jeffrey Hulten
jhulten at gmail.com
Mon Oct 2 16:16:07 PDT 2006
So I am trying to use ActiveRecord in a series of Rake tasks for work and I
am running into some issues. When I run my script I get the warnings below
and then the script hangs. It is hanging on a simple find(1). Any help
would be appreciated.
sulla:~/Projects/ASD/INF_0609R2 jeffh$ rake test:models
(in /Users/jeffh/Projects/ASD/INF_0609R2)
/usr/bin/ruby -w
-I/Users/jeffh/Projects/ASD/INF_0609R2/common/release/lib:/Users/jeffh/Projects/ASD/INF_0609R2/common/release/lib/models
"/usr/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb"
"test/test_model_program.rb"
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:334:
warning: `*' interpreted as argument prefix
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/validations.rb:363:
warning: `*' interpreted as argument prefix
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:224:
warning: instance variable @ignore_new_methods not initialized
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:224:
warning: instance variable @ignore_new_methods not initialized
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:41:
warning: method redefined; discarding old allow_concurrency=
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/sqlserver_adapter.rb:456:
warning: method redefined; discarding old remove_column
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/oracle_adapter.rb:119:
warning: (...) interpreted as grouped expression
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/reflection.rb:120:
warning: instance variable @primary_key_name not initialized
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/reflection.rb:120:
warning: instance variable @primary_key_name not initialized
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/reflection.rb:120:
warning: instance variable @primary_key_name not initialized
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.7.1
/lib/rake/rake_test_loader
Started
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/connection_specification.rb:74:
warning: instance variable @active_connection_name not initialized
The model:
require 'ar_connection'
class Program < ActiveRecord::Base
has_many :branches
#has_many :current_months_branches,
# :class_name => 'Branch',
# :finder_sql => "select * from branches b, programs p " +
# "where p.id = b.program_id " +
# "and b.name like '%\_#{
Time.now.strftime("%y%m")}R%"
belongs_to :program_manager,
:class_name => 'Person',
:foreign_key => 'pm_id'
def make_branch
name = "#{self.short_name}_#{Time.now.strftime("%y%m")}R#{
self.current_months_branches.count + 1}"
puts "Creating branch: #{name}"
#TODO: Actual branch creation logic
end
end
The connection file:
require 'rubygems'
require 'active_record'
conn = ActiveRecord::Base.establish_connection(
:adapter => 'mysql',
:host => 'host.company.com',
:port => 3307,
:database => 'tools_dev',
:username => 'ME',
:password => 'MINE'
)
The model test:
require 'test/unit'
require 'program'
class TestModelPrograms < Test::Unit::TestCase
def test_find
program = Program.find(1)
assert_equal('MAINT', program.short_name)
end
end
--
Jeffrey Hulten
jhulten at gmail.com
More information about the Ruby
mailing list