Method in Model

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’

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

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?

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

I have also just included <%= RAILS_ENV.to_s %> into an rhtml and it
shows development when I refresh the page.

Any other ideas??

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

Let us know how it goes! :slight_smile:

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.

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

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

Let us know how it goes! :slight_smile:

Peter