# RE: Object constructors - Noob Question

**URL:** <https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326>\
**Category:** Rails\
**Created:** [May 12, 2006, 4:09am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326 "2006-05-12T04:09:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 12, 2006, 4:09am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/1 "2006-05-12T04:09:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 13, 2006, 12:33am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/2 "2006-05-13T00:33:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 13, 2006, 12:43am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/3 "2006-05-13T00:43:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 13, 2006, 12:49am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/4 "2006-05-13T00:49:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 13, 2006, 12:45am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/5 "2006-05-13T00:45:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 14, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/6 "2006-05-14T19:31:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 15, 2006, 5:32am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/7 "2006-05-15T05:32:45Z")

</div>

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/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](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

---

<div class="post-metadata">

**Author:** ![Ross\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b19c9b/32.png) [@Ross\_D](https://www.ruby-forum.com/u/Ross_D)\
**Post date:** [May 15, 2006, 8:01am UTC](https://www.ruby-forum.com/t/re-object-constructors-noob-question/60326/8 "2006-05-15T08:01:22Z")

</div>

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.
