invoke rails model from other ruby files

J

Junkone

how can i invoke rails model objects from other non rails ruby
objects. do i have to do a require '<railsobject>' or is there a way i
can refernce rails enviorument from ruby.
 
J

James Britt

Junkone said:
how can i invoke rails model objects from other non rails ruby
objects. do i have to do a require '<railsobject>' or is there a way i
can refernce rails enviorument from ruby.

Browse around for past messages on using ActiveRecord apart from the
rest of Rails.

I've not done it, but it's doable.
 
M

Marcin Raczkowski

datab.rb:


require 'rubygems'
require 'activerecord'
require 'yaml'
require 'logger'

class DataB
def connect_to_db(environment="development")
conf = YAML::load(File.open(File.dirname(__FILE__) +
/config/database.yml'))
ActiveRecord::Base.logger = Logger.new("log/active_record.log")
ActiveRecord::Base.establish_connection(conf[environment])
end

def load_models()
["model/list.rb",
"model/models.rb","model/here.rb","model/or_use_File.glob"].each do |lib|
require lib
end
end
end

---------------------------
@db=DataB.new
@db.connect_to_db
@db.load_models

and you're ready to go!
 
J

Junkone

Browse around for past messages on using ActiveRecord apart from the
rest of Rails.

I've not done it, but it's doable.

--
James Britt

"I can see them saying something like 'OMG Three Wizards Awesome'"
- billinboston, on reddit.com

unfortunatly this is the error i get uninitialized constant
ActiveRecord. any clues how to fix it.


irb(main):010:0> require 'E:\TradingTools\torontotrader\app\models
\rawdata.rb'
NameError: uninitialized constant ActiveRecord
from E:\TradingTools\torontotrader\app\models\rawdata.rb:1
from e:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `ge
m_original_require'
from e:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `re
quire'
from (irb):10
 
J

Junkone

Browse around for past messages on using ActiveRecord apart from the
rest of Rails.

I've not done it, but it's doable.

--
James Britt

"I can see them saying something like 'OMG Three Wizards Awesome'"
- billinboston, on reddit.com

Unfortunately this is the error that i get uninitialized constant
ActiveRecord

irb(main):010:0> require 'E:\Trad\app\models\rawdata.rb'
NameError: uninitialized constant ActiveRecord
from e:\Trad\app\models\rawdata.rb:1
from e:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `ge
m_original_require'
from e:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
27:in `re
quire'
from (irb):10
 
J

Junkone

datab.rb:

require 'rubygems'
require 'activerecord'
require 'yaml'
require 'logger'

class DataB
def connect_to_db(environment="development")
conf = YAML::load(File.open(File.dirname(__FILE__) +
/config/database.yml'))
ActiveRecord::Base.logger = Logger.new("log/active_record.log")
ActiveRecord::Base.establish_connection(conf[environment])
end

def load_models()
["model/list.rb",
"model/models.rb","model/here.rb","model/or_use_File.glob"].each do |lib|
require lib
end
end
end

---------------------------
@db=DataB.new
@db.connect_to_db
@db.load_models

and you're ready to go!

thanks. it works
 
J

Junkone

You are welcome. I know :D

hello
Since your code had a include 'logger' in it, i assume that i can use
it to log errors to the rails log. however it does not seem to work
the way i imagined. can u point what the issue could be. here is the
snippet of cde and the exception.

require 'rubygems'
require 'active_record'
require 'yaml'
require 'logger'
....... lots of code

rescue => detail
Logger.error( detail.backtrace.join("\n"))

i get the error

undefined method `error' for Logger:Class
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top