Invoke rails model from other ruby files

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.

Junkone wrote:

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.

···

--
James Britt

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

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!

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

···

On Nov 8, 1:27 pm, James Britt <james.br...@gmail.com> wrote:

Junkone wrote:
> 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.

--
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

···

On Nov 8, 1:27 pm, James Britt <james.br...@gmail.com> wrote:

Junkone wrote:
> 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.

--
James Britt

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

thanks. it works

···

On Nov 8, 1:53 pm, Marcin Raczkowski <mailing...@gmail.com> wrote:

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

You are welcome. I know :smiley:

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

···

On Nov 8, 8:31 pm, Marcin Raczkowski <mailing...@gmail.com> wrote:

> thanks. it works

You are welcome. I know :smiley: