# Lazy Loading?

**URL:** <https://www.ruby-forum.com/t/lazy-loading/74063>\
**Category:** Rails\
**Created:** [December 21, 2005, 2:52am UTC](https://www.ruby-forum.com/t/lazy-loading/74063 "2005-12-21T02:52:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 21, 2005, 2:52am UTC](https://www.ruby-forum.com/t/lazy-loading/74063/1 "2005-12-21T02:52:58Z")

</div>

I’m having problems with what seems to be lazy loading behavior in Ruby  
on Rails. I have two objects in a parent-child relationship such as:

class Order \< ActiveRecord::Base  
has\_many :items, :dependent =\> true  
end  
class Item \< ActiveRecord::Base  
belongs\_to :order  
end

I put an instance of the Order object into the session using:

session[:order] = Order.new

I then extract the object in the controller and set it equal to the  
instance variable @order so that it is accessible in my view.

def myaction  
@order = session[:order]  
end

Now, my problem is that, in the view, the items array in @order is nil  
UNLESS I access the items array in the controller, e.g.,

def myaction  
@order = session[:order]  
logger.debug(@order.items)  
end

This seems to me that Rails is loading the items array in a lazy fashion  
(which is great and expected), however, for some reason it _only_ does  
this in the controller. That is, if I access the items array in the  
view, it doesn’t trigger the loading of the array. Am I doing something  
wrong here? Is there a way to control the lazy loading behavior of  
Rails? Help!

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 21, 2005, 5:44am UTC](https://www.ruby-forum.com/t/lazy-loading/74063/2 "2005-12-21T05:44:14Z")

</div>

Jason,

It’s been my experience that if you’re saving models into a session  
(as opposed to just the models’ unique id), then you can’t lazy-load  
them.

In your application.rb file, try adding model :order

See the Rails wiki article [1] for more info on session handling.

[1] [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/HowtoWorkWithSessions/)

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 21, 2005, 2:44pm UTC](https://www.ruby-forum.com/t/lazy-loading/74063/3 "2005-12-21T14:44:50Z")

</div>

Thanks for the response, Ed…

I did have the model :order set in the application.rb file as well as  
model :line, however, it still did not work. Lazy-loading of the items  
did work in the controller, when the object was retrieved from the  
session, just not in the view.

I’ve changed my approach to just store the id of the order in the  
session and then load the order in my controller with the extra :include  
=\> :items parameter, e.g.,

@order = Order.find(:all, conditions =\> “id = #{session[:order\_id]}”,  
:include =\> items)

I found out that this extra parameter will tell Rails to load the items  
array when the order object is loaded instead of loading the items in a  
lazy fashion. I then use the @order object in my view and it works  
fine.

I would, however, still like to understand the details of how Rails does  
lazy loading. So if anyone out there knows, please drop me a line.  
Thanks!

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 21, 2005, 2:48pm UTC](https://www.ruby-forum.com/t/lazy-loading/74063/4 "2005-12-21T14:48:40Z")

</div>

> @order = Order.find(:all, conditions =\> “id = #{session[:order\_id]}”,  
> :include =\> items)

Whoa, hold on there cowboy. Coding like that can get you an sql  
injection attack. (Unlikely with a session value, but you don’t want  
to get in the habit).

@order = Order.find(:all, :conditions =\> [‘id = ?’,  
session[:order\_id]], :include =\> :items)

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)
