Problem with classes

Andi Scharfstein <[email protected]> Tue, 9 Aug 2005 21:15:36 +0200
Newsgroups gmane.comp.apache.mod-ruby
Message-ID <[email protected]>
Hi,
I'm trying to switch from XML to Berkeley DB for the purpose of
saving comments entered on my website, and encountered some problems.
I'm saving my data in a class I defined, which I believe is a standard
way of doing this under Berkeley DB (though I could be wrong) and
read it in again when I need it. However, when I do this, Apache
displays a 503 error, and the log says:

   [error] mod_ruby: error in ruby:
   in `load': undefined class/module Entry (ArgumentError)

Well, "Entry" is defined in the same file as the code using it, and in
fact it works flawlessly when invoked from the command line - it's
just when I'm using Apache that I get this error. Does anybody know
what might be the cause of this curious behaviour? Thanks for any
input on this.

Here's the file (minus the <% %>) that fails for me:

require "cgi"
require "bdb"

class Entry
        attr_writer :comments
        attr_reader :comments, :entry_id
        
        def initialize(entry_id)
                @entry_id = entry_id
                @comments = Array.new
        end
        
        def to_s
                "#@entry_id: #{comments.size} Kommentare"
        end
end

class Comment
        attr_writer :time
        attr_reader :time, :body, :name_link
        
        def initialize(name, email, url, body)
                @time = Time.now
                @timestamp = Time.now.strftime("%a, %d.%m.%y um %H:%M:%S")
                @name = name
                @email = email
                @url = url
                @body = body
                if url != ''
                    @name_link = "<a target=\"_blank\" href=\"#{url}\">#{name}</a>"
            else
                    @name_link = name
                end
        end
        
        def to_s
      "#@name (#@email, #@url) at #@time:\n#@body"
        end
end

def link_comments(id)
  db = BDB::Btree.open "data/comments", nil, BDB::CREATE, 0644

###### The following line will fail: #######
  
  comment_number = Marshal.load(db[id]).comments.size
  db.close

  comment_link_text = "Kommentare (#{comment_number})"
  comment_link = "<a href=\"comments.html?entry_id=#{CGI.escapeHTML(id)}\" onclick=\"OpenComments(this.href); return false\">#{comment_link_text}</a>"

  return comment_link
end

-- 
Bye: Andi S.                          mailto:[email protected]