Forum: Ruby on Rails RE: Object constructors - Noob Question

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.
Ross D. (Guest)
on 2006-05-12 06:09
(Received via mailing list)
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
Joe C. (Guest)
on 2006-05-13 02:33
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.
Ezra Z. (Guest)
on 2006-05-13 02:43
(Received via mailing list)
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
Joe C. (Guest)
on 2006-05-13 02:45
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.
Joe C. (Guest)
on 2006-05-13 02:49
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.
Joe C. (Guest)
on 2006-05-14 21:31
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.
Ezra Z. (Guest)
on 2006-05-15 07:32
(Received via mailing list)
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
Joe C. (Guest)
on 2006-05-15 10:01
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.
This topic is locked and can not be replied to.