# Reload

**URL:** <https://www.ruby-forum.com/t/reload/87706>\
**Category:** Rails\
**Created:** [March 5, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/reload/87706 "2007-03-05T12:39:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@prabu](https://www.ruby-forum.com/u/prabu)\
**Post date:** [March 5, 2007, 12:39pm UTC](https://www.ruby-forum.com/t/reload/87706/1 "2007-03-05T12:39:15Z")

</div>

whats the use for reload? and how it works ?

---

<div class="post-metadata">

**Author:** ![prabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@prabu](https://www.ruby-forum.com/u/prabu)\
**Post date:** [March 5, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/reload/87706/2 "2007-03-05T12:49:16Z")

</div>

On Mar 5, 2007, at 12:37 PM, prabu wrote:

> whats the use for reload? and how it works ?

Class reloading, if enabled, is triggered by some code at the end of  
each request cycle.

In the console, however, there are no requests going on so code  
changes are not refreshed. The reload! method is offered as a way to  
refresh classes manually.

– fxn

---

<div class="post-metadata">

**Author:** ![prabu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f1d935/32.png) [@prabu](https://www.ruby-forum.com/u/prabu)\
**Post date:** [March 5, 2007, 3:52pm UTC](https://www.ruby-forum.com/t/reload/87706/3 "2007-03-05T15:52:46Z")

</div>

prabu wrote:

> whats the use for reload? and how it works ?

Try this test case:

```
assert_difference @tygr.props, :count, -1 do
  xhr :get, 'apply_prop', :prop_id => potion.id, :user_id => 

```

@tygr.id  
@tygr.reload  
end

Before calling xhr :get, the @tygr model object has one prop. xhr :get  
tests the ‘apply\_prop’ action, which must load its own copy of the  
same tygr model object, to consume its prop and save the new list.

So, before assert\_difference detects the prop count increases by -1,  
the @tygr object gets a reload to refresh its prop list.

Maybe @tygr.props.reload would be cleaner. And if my tests have too  
many reloads, I should bottle them up, like this:

def assert\_model\_difference(model, method, difference)  
assert\_difference model, method, difference do  
yield  
model.reload  
end  
end

…

```
assert_model_difference @tygr.props, :count, -1 do
  xhr :get, 'apply_prop', :prop_id => potion.id, :user_id => 

```

@tygr.id  
end

Those are all test-side reloads, because the testing code often cannot  
pass the same model object into the production code as it will use.  
sessions and params should only pass IDs. So we get an “alias”  
situation, with two in-memory copies of the same model object  
identity, and we need save on one side and reload on the other to  
synchronize them.

In production code, I suspect that too many ‘reload’ calls are a  
“design smell” - a hint that something else in the design should  
improve. An event-driven program should not alias the same model  
objects all over the place; it should load them up once, mess with  
them once, and save them once.

I will crack down on y’all abusing that guideline as soon as I figure  
out how to follow it myself!

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!
