# Session trouble

**URL:** <https://www.ruby-forum.com/t/session-trouble/90247>\
**Category:** Rails\
**Created:** [March 27, 2007, 1:25am UTC](https://www.ruby-forum.com/t/session-trouble/90247 "2007-03-27T01:25:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 27, 2007, 1:25am UTC](https://www.ruby-forum.com/t/session-trouble/90247/1 "2007-03-27T01:25:24Z")

</div>

I am having a problem with an assignment to an object stored in session.

for example when I am in console

o = NewOrder.new

# dl = DeliveryLocation.new

# o.delivery\_location = dl #=\> returns a delivery location object

# o.delivery\_loaction.save #=\> returns true

when I am using the controller

delivery\_location = DeliveryLocation.new(params[:delivery\_location])  
session[:order].delivery\_location = delivery\_location  
session[:order].delivery\_location.save

When it gets to session[:order].delivery\_location = delivery\_location I  
get the error  
You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.klass

/association\_proxy.rb:147:in `raise_on_type_mismatch' /associations/has_one_association.rb:44:in`replace’  
/associations.rb:900:in `delivery_location=' /delivery_locations_controller.rb:21:in`create’

my Order model has\_one :delivery\_location  
my DeliveryLocation model belongs\_to :order

Any suggestions would be awesome.

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 27, 2007, 3:16am UTC](https://www.ruby-forum.com/t/session-trouble/90247/2 "2007-03-27T03:16:19Z")

</div>

write a test suite

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 27, 2007, 3:19am UTC](https://www.ruby-forum.com/t/session-trouble/90247/3 "2007-03-27T03:19:49Z")

</div>

It seems that if I change my association to

class Order  
has\_many :delivery\_locations  
end

class DeliveryLocation  
belongs\_to :order  
end

then just append the delivery \_location object into the  
delivery\_locations array then it works… BUT, this is not what I  
want.

Another example I seen was on the [dev.rubyonrails.org](http://dev.rubyonrails.org)

class Citizen \< ActiveRecord::Base  
has\_one :ss\_number  
end

class SSNumber \< ActiveRecord::Base  
belongs\_to :citizen  
end

If I want this, but have to change it to Citizen has\_many :ss\_numbers  
just to work there is a problem.

Looking through the rails files, it seems what is dying is  
def raise\_on\_type\_mismatch(record)  
unless record.is\_a?(@reflection.klass)

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [April 15, 2007, 4:17am UTC](https://www.ruby-forum.com/t/session-trouble/90247/4 "2007-04-15T04:17:23Z")

</div>

Hi Jeremy, I came across your post and was having a similar problem. To  
solve it I found that I had to initialize my association (also a  
belongs\_to) when first storing the object in the session (in your case  
session[:order]). In other words when first assigning session[:order]  
make sure that the delivery\_location is not nil. You may not have that  
data collected yet (in my case I have multi-step wizard with a complex  
object graph), but just be sure that the belongs\_to relationship is  
initialized. Otherwise Rails seems to throw that nil.klass error when  
calling raise\_on\_type\_mismatch because nil isn’t the same type as  
DeliveryLocation. Go figure.

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [April 15, 2007, 4:28am UTC](https://www.ruby-forum.com/t/session-trouble/90247/5 "2007-04-15T04:28:17Z")

</div>

Hi Jeremy, there’s no attribute or attr\_writer on the session called

delivery\_location=

If you’re trying to simply save an object into a session, you can do  
the following:

session[:order] = o

Now, you can pull it out by doing the following:

order = session[:order]

If you require further information, I would recommend referencing the  
relevant section in AWDwRv2.

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [March 27, 2007, 4:06am UTC](https://www.ruby-forum.com/t/session-trouble/90247/6 "2007-03-27T04:06:43Z")

</div>

Keynan P. wrote:

> write a test suite

Thanks. It didn’t show me anything different, but I did write a test and  
I get the same thing.
