[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Basic ActiveRecord Usage

jotto

10/14/2006 4:51:00 AM

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

1 Answer

Andrew Libby

10/14/2006 1:02:00 PM

0


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


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


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



jotto wrote:
> 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
>
>

--
Andrew Libby
Tangeis, LLC
Innovative IT Management Solutions
alibby@tangeis.com