RE: Object constructors - Noob Question


#1

Joe wrote

way?
@foo = Foo.new # this is redundant you don’t need to create an object
@foo = session[:foo] # just get the already created one out of the
session

Not a Ruby developer yet but I suspect you could

@bar = session[:foo].bar

Which would be about as lean as you could get

Ross


#2

Ross D. wrote:

Joe wrote

way?
@foo = Foo.new # this is redundant you don’t need to create an object
@foo = session[:foo] # just get the already created one out of the
session

Not a Ruby developer yet but I suspect you could

@bar = session[:foo].bar

Which would be about as lean as you could get

Ross

Hi and thanks! However, I was doing that way and it caused this error
on invoking the second action:

“Session contains objects whose class definition isn’t available.
Remember to require the classes for all objects kept in the session.
(Original exception: uninitialized constant Generic [NameError])”

I have been told to add a model line in every controller where I
reference that object in the session, but I have added the code below to
all of my controllers with no change of behavior:
Model :foo

I ended up removing the object from the session and only save foo_id,
but then I need to foo.find(session[:foo_id]) on every controller where
I need foo. This will cause a lot of unneeded database io.


#3

On May 12, 2006, at 3:33 PM, Joe C. wrote:

I have been told to add a model line in every controller where I
reference that object in the session, but I have added the code
below to
all of my controllers with no change of behavior:
Model :foo

that needs to be :

model :foo

not upcase.

-Ezra


#4

Joe C. wrote:

Ezra Z. wrote:

On May 12, 2006, at 3:33 PM, Joe C. wrote:

I have been told to add a model line in every controller where I
reference that object in the session, but I have added the code
below to
all of my controllers with no change of behavior:
Model :foo

that needs to be :

model :foo

not upcase.

-Ezra

Hi, it is lower case in code, and looking back on my message, lower case
there as well.

Hi sorry “Model” is upercase in the message only… thought you were
talking about :foo.


#5

Ezra Z. wrote:

On May 12, 2006, at 3:33 PM, Joe C. wrote:

I have been told to add a model line in every controller where I
reference that object in the session, but I have added the code
below to
all of my controllers with no change of behavior:
Model :foo

that needs to be :

model :foo

not upcase.

-Ezra

Hi, it is lower case in code, and looking back on my message, lower case
there as well.


#6

Hi, I’ve narrowed this problem down further, and I probably should have
mentioned a few of these details before.

“bar” is inherited from “thing” in a single table inheritance structure.

I got rid of the error when I took “bar” out of the picture. So I can
do the following for example:
@foo = session[:foo]
puts @foo.id

The second I reference “bar” I get zapped with the above message (i.e.
bar = @foo.bar). Here is how the models are set up:

class Thing < ActiveRecord::Base
belongs_to :foo
end
class Bar < Thing
end
class Yadda < Thing
end

class Foo < ActiveRecord::Base
has_many :things
has_one :yadda
has_one :bar
validates_uniqueness_of :name
end

This works the first time that I call @foo.bar (no errors, no warnings),
but after the first call all I get is a 500 error and the following in
the log file:
“Session contains objects whose class definition isn’t available.
Remember to require the classes for all objects kept in the session.
(Original exception: uninitialized constant Generic [NameError])”

In all of my controllers I’ve included:
requires ‘foo.rb’
requires ‘thing.rb’
model :foo
model :thing

I’m not sure if I’m doing something wrong or if I’m running into some
esoteric bug.


#7

Hi!

On May 14, 2006, at 10:31 AM, Joe C. wrote:

puts @foo.id
end
but after the first call all I get is a 500 error and the following in

I’m not sure if I’m doing something wrong or if I’m running into some
esoteric bug.


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

I wonder if you are running into rails autorequire feature. One

thing to try is to split up your Thing, Bar and Yadda model classes
into three separate files called thing.rb, foo.rb and yadda.rb. Try
this and see if it helps the problem. You might still need the
model :foo declarations as well.

-Ezra


#8

Ezra Z. wrote:

Hi!

On May 14, 2006, at 10:31 AM, Joe C. wrote:

I wonder if you are running into rails autorequire feature. One
thing to try is to split up your Thing, Bar and Yadda model classes
into three separate files called thing.rb, foo.rb and yadda.rb. Try
this and see if it helps the problem. You might still need the
model :foo declarations as well.

-Ezra

Hi and thanks, I tried this right away as it seemed to be a good
solution, but the exact same behavior is happening. It works the first
time, and the second I get the 500 error. The only way to get the app
running again is to delete the session or clear the browser cache.

The very weird thing about this is the message in the log file
mentioning the session. The error only occurs when I introduce bar into
the equation, running @bar = @foo.bar and bar never touches the session
at all.

@foo is instantiated from the session, by @foo = session[:foo] but this
doesn’t cause the error. In fact I can use @foo in views and other
ways and no error occurs.

I really have no idea what’s going on with this.