Basic ActiveRecord Usage

J

jotto

I want to run a Ruby script outside of Rails and still use Active
Record. This is what I'm trying but I am getting a "const_missing"
error - do I need to reference the connection in the printlisting
method?:

require 'rubygems'
require 'active_record'

class TheMode < ActiveRecord::Base
def initialize
end

def printlisting
ActiveRecord::Base.establish_connection:)adapter => "mysql", :host =>
"localhost", :username => "root", :password => "root", :database =>
"commentstable")
listing = Comment.find:)all)
puts listing.text
end

temp = TheMode.new
temp.modemaker
 
A

Andrew Libby

I found this Wiki article, which helped me out a lot.


http://wiki.rubyonrails.org/rails/pages/HowToUseActiveRecordOutsideRails


New to Ruby, but I have a theory. When ever I've done this,
I typically run ActiveRecord::Base.establish_connection
outside of any class definition, that seemed to work for me.
Another approach I've used (when some of my models in rails
connection to a different DB than the rest of the rails app)
is to have a top level class (called DB) that is something like

class DB < ActiveRecord::Base
ActiveRecord::Base.establish_connection( .. )
end

And then TheMode and Comment would be

class TheMode < DB
end

class Comment < DB
end

Good luck.

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top