[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

invoke rails model from other ruby files

Junkone

11/8/2007 6:08:00 PM

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.

7 Answers

James Britt

11/8/2007 6:27:00 PM

0

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

Marcin Raczkowski

11/8/2007 6:53:00 PM

0

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!

Junkone

11/8/2007 6:54:00 PM

0

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

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



Junkone

11/8/2007 8:27:00 PM

0

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


Junkone

11/9/2007 1:11:00 AM

0

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

Marcin Raczkowski

11/9/2007 1:31:00 AM

0

> thanks. it works
You are welcome. I know :D



Junkone

11/9/2007 10:06:00 PM

0

On Nov 8, 8:31 pm, Marcin Raczkowski <mailing...@gmail.com> wrote:
> > thanks. it works
>
> 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