Forum: Ruby on Rails ActiveRecord: Can't store a class derived from AR::Base

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel Völkerts (Guest)
on 2009-01-10 17:30
(Received via mailing list)
Hi,

try to use ActiveRecord for my little standalone application, without
the rails environment. Here is my first try

8<--------------------------------------------------

# Main
if __FILE__ == $0 then

   ActiveRecord::Base.establish_connection(
     :adapter =>  'mysql',
     :host => @config[:sql][:hostname],
     :database => @config[:sql][:database],
     :username => @config[:sql][:username],
     :password => @config[:sql][:password]
   )

   puts 'Erzeuge ein paar Knoten und speicher diese in der DB: '

   80.times do |i|
     aNode = Node.new("node_#{i}",1)
     print '.'
     aNode.save!
   end

end

8<--------------------------------------------------

@config is well initialized, the ActiveRecord connects to my DB as
you'll expected. But the call aNode.save! fails with this error:

Erzeuge ein paar Knoten und speicher diese in der DB:
.c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:164:in
`rollback_active_record_state!': undefined method `delete' for
nil:NilClass (NoMethodError)
         from
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/transactions.rb:150:in
`save!'
         from main.rb:35
         from main.rb:32:in `times'
         from main.rb:32

Mmm, but I have looked in transactions.rb at line 150 and 164, looks
fine for me. My AR gems are

D:\ramantics>gem list --local

*** LOCAL GEMS ***

actionmailer (2.2.2)
actionpack (2.2.2)
activerecord (2.2.2)
activeresource (2.2.2)
activesupport (2.2.2)
fxri (0.3.7, 0.3.6)
fxruby (1.6.17, 1.6.6)
hpricot (0.6.164, 0.4)
log4r (1.0.5)
mysql (2.7.3)
ptools (1.1.6)
rails (2.2.2)
rake (0.8.3, 0.7.2)
rubygems-update (1.3.1)
sources (0.0.1)
test-unit (2.0.2)
win32-api (1.2.2)
win32-clipboard (0.4.4, 0.4.1)
win32-dir (0.3.2, 0.3.1)
win32-eventlog (0.5.0, 0.4.3)
win32-file (0.6.0, 0.5.3)
win32-file-stat (1.3.2, 1.2.3)
win32-process (0.6.0, 0.5.1)
win32-sapi (0.1.4, 0.1.3)
win32-sound (0.4.1, 0.4.0)
windows-api (0.2.4)
windows-pr (0.9.8, 0.6.2)

Has anyone an idea where I made the mistake? Thanks a lot.

g,

Daniel
Frederick C. (Guest)
on 2009-01-10 21:43
(Received via mailing list)
On Jan 9, 9:26 pm, Daniel Völkerts <removed_email_address@domain.invalid> wrote:

> Has anyone an idea where I made the mistake? Thanks a lot.

Have you override initialize in you Node class ?

Fred
Daniel Völkerts (Guest)
on 2009-01-11 13:33
(Received via mailing list)
Frederick C. schrieb:

> Have you override initialize in you Node class ?

Hi Fred,


aaah yes. I already found the problem. As you mentioned, I overwrote
initialize in my Node class which inherits AR::Base.

Damn I didn't know that this isn't allowed :( Fixed failures lead to
better code *g*

Thanks for your response.

Daniel

--
“Das ich erkenne, was die Welt im Innersten zusammenhält” (Faust I)
Frederick C. (Guest)
on 2009-01-11 16:21
(Received via mailing list)
On 11 Jan 2009, at 11:33, Daniel Völkerts wrote:

>
> Damn I didn't know that this isn't allowed :( Fixed failures lead to
> better code *g*
>
You can override initialize, but you just need to ensure you call the
superclass implementation properly.

Fred
Daniel Völkerts (Guest)
on 2009-01-11 22:06
(Received via mailing list)
Frederick C. schrieb:

> You can override initialize, but you just need to ensure you call the
> superclass implementation properly.

Oh, thanks for this additional information, I see I've to read the docs
,)

g,

Daniel

--
“Das ich erkenne, was die Welt im Innersten zusammenhält” (Faust I)
This topic is locked and can not be replied to.