# Method in Model

**URL:** <https://www.ruby-forum.com/t/method-in-model/68627>\
**Category:** Rails\
**Created:** [September 14, 2006, 12:40am UTC](https://www.ruby-forum.com/t/method-in-model/68627 "2006-09-14T00:40:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 14, 2006, 12:40am UTC](https://www.ruby-forum.com/t/method-in-model/68627/1 "2006-09-14T00:40:32Z")

</div>

I am runnning through the Agile web development with Rails book…and  
have come across the following problem.

Everything has been going great until I get the point where I am  
supposed to be adding a price\_total to the add\_to\_cart.rhtml.

Now when I add to the cart I get undefined method “price\_total” for  
#Cart:blahblah. I thought maybe I mistyped something, so I went and  
checked everything again and again, same problem. I tried everything I  
could think of, tried rake db:sessions:clear, same problem. Tried  
putting the method price\_total somewhere else…same problem.

Finally just for fun (really didn’t expect it to fix it) I restarted  
webrick. THIS FIXED IT!

I have now worked out that if I change the method name (both in the  
model and the rhtml) it will not assosciate until AFTER I restart  
webrick. Also, if I add a new method it will not be accessible until I  
restart webrick. Additionally, if I create a method, interact with the  
website, then delete the method from the model the information from that  
deleted method will still show up on the website until AFTER I restart  
webrick.

It seems that any change I make to the model doesn’t happen until after  
I restart webrick. Any ideas on this? In case it matters the cart  
model is just ‘class Cart’ not something like ‘class Cart \<  
Somethingelse’

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 14, 2006, 12:58am UTC](https://www.ruby-forum.com/t/method-in-model/68627/2 "2006-09-14T00:58:46Z")

</div>

Hi Joe,

It sounds like you’re running WebRICK in “production” mode, not  
“development” mode… in “production” mode, Rails will only load your  
model classes _once_, whereas in “development” mode, it will reload  
your model classes on each and every HTTP request… check your  
environment (“RAILS\_ENV”) and/or your config files, and ensure it’s  
set to “development” mode.

Good luck,

Peter

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 14, 2006, 1:07am UTC](https://www.ruby-forum.com/t/method-in-model/68627/3 "2006-09-14T01:07:12Z")

</div>

Peter Vandenberk wrote:

> Hi Joe,
> 
> It sounds like you’re running WebRICK in “production” mode, not  
> “development” mode… in “production” mode, Rails will only load your  
> model classes _once_, whereas in “development” mode, it will reload  
> your model classes on each and every HTTP request… check your  
> environment (“RAILS\_ENV”) and/or your config files, and ensure it’s  
> set to “development” mode.
> 
> Good luck,
> 
> Peter  
> Someone recommended that on an irc channel I was on, but it was hard to  
> follow the conversation because like 20 people were having their own  
> conversation.

I wasn’t sure how to check if it was in production/development so I just  
looked at the logs and development log was long where production was  
completely empty. How should I be checking which mode it is in?

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 14, 2006, 1:17am UTC](https://www.ruby-forum.com/t/method-in-model/68627/4 "2006-09-14T01:17:13Z")

</div>

Ok I have done ruby script/console and typed RAILS\_ENV and it came back  
with development…Was this the way to do it?

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 14, 2006, 1:26am UTC](https://www.ruby-forum.com/t/method-in-model/68627/5 "2006-09-14T01:26:54Z")

</div>

I have also just included \<%= RAILS\_ENV.to\_s %\> into an rhtml and it  
shows development when I refresh the page.

Any other ideas??

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 14, 2006, 5:10am UTC](https://www.ruby-forum.com/t/method-in-model/68627/6 "2006-09-14T05:10:00Z")

</div>

Peter Vandenberk wrote:

> > That syntax is now deprecated in favor of adding the following  
> > declaration in your model file:
> > 
> > include Reloadable
> 
> See also: [http://clarkware.com/cgi/blosxom/2006/03/28](http://clarkware.com/cgi/blosxom/2006/03/28)
> 
> Let us know how it goes! 🙂
> 
> Peter

That worked GREAT! This is probably something they should have included  
in the book…Well, I guess it being a “beta book” so far maybe they just  
haven’t caught it. Dunno…but that worked great, I can now add to that  
model on the fly without restarting.

Thanks a lot! I’m going to let them know on the pragprog site.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 15, 2006, 3:50am UTC](https://www.ruby-forum.com/t/method-in-model/68627/7 "2006-09-15T03:50:55Z")

</div>

Thank you sooooo much for asking this question. I have had the exact  
same problem today and have wasted about 6 hours including all the  
frustration.

Also, thanks to all who have taken the time to answer.

Phil

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [September 14, 2006, 1:38am UTC](https://www.ruby-forum.com/t/method-in-model/68627/8 "2006-09-14T01:38:49Z")

</div>

Hi Joe,

On 14 Sep 2006, at 00:17, Joe wrote:

> Ok I have done ruby script/console and typed RAILS\_ENV and it came  
> back  
> with development…Was this the way to do it?

Yes, that’s one way to do it … also, as per your previous post, if  
the development.log is growing but the production.log stays empty,  
then Rails is definitely running in development mode. I just re-read  
your original post in more detail, and noticed your very last  
comment, which I missed the first time around:

> In case it matters the cart  
> model is just ‘class Cart’ not something like ‘class Cart \<  
> Somethingelse’

Yes, it matters quite a lot, actually. Rails will only reload AR  
model classes, ie. classes that inherit from ActiveRecord::Base…  
for non-ActiveRecord model classes, you need to help Rails out a bit:

> If you want Rails to reload a model that isn’t a subclass of  
> ActiveRecord::Base, you used to add the following declaration in a  
> controller:
> 
> model :my\_model  
> That syntax is now deprecated in favor of adding the following  
> declaration in your model file:
> 
> include Reloadable

See also: [http://clarkware.com/cgi/blosxom/2006/03/28](http://clarkware.com/cgi/blosxom/2006/03/28)

Let us know how it goes! 🙂

Peter
