Why do I have to reload the Console

I am new to RoR and trying to learn my way around. I have Bruce Tate’s
RoR Up And Running book and am using it to learn RoR.
An example in chapter 2 page 34 shows how simple it is to add
validation for table records. The example adds validation to the
ActiveRecord model class Photo.rb as follows:

Class without validation:
class Photo < ActiveRecord::Base
end

Class with validation:
class Photo < ActiveRecord::Base
validates_presence_of :filename
end

The MySQL table that this class is managing is as follows:

mysql> select * from photos;
±—±---------+
| id | filename |
±—±---------+
| 1 | Cat.jpg |
| 2 | dog.jpg |
±—±---------+
2 rows in set (0.00 sec)

Now I am trying to test all this from RoR console as the book is
instructing me to. It says to type in the following in the console
photo=Photo.new
photo.save
At this point, the validation is supposed to kick in and I am supposed
to get a false reply. Instead I got true and indeed, a new record with
empty filename was saved to the photos table. I was wondering as to
why the validation did not work until I spotted the text written in the
book which said that I will have to restart the console to reload my
changes. I did that and the validation started to work.
Is there something that I don’t know? I thought that the development
environment was supposed to reload your changes instantly. I know that
it is true for the Web Server. Why do I have to exit the console and
reload it to see my changes? Is there a better way to reload the
environment without having to quit the console and having to retype
“ruby script/console” and wait for it to reload?
Thanks in advance for your reply.
bharat

Bharat,

You can reload a class by typing

load ‘classname.rb’

in the console…

Nathan

Thanks Nathan. As you can tell, I am really new to Rails. Is there a
place/book/tutorial where I can find answers for console related
questions? I find the console to be very useful tool while learning
and would like to know all the commands and tricks like the one you
mention above.
Regards,
Bharat

On Wed, 8 Nov 2006, Bharat wrote:

Is there something that I don’t know? I thought that the development
environment was supposed to reload your changes instantly. I know that
it is true for the Web Server. Why do I have to exit the console and
reload it to see my changes? Is there a better way to reload the
environment without having to quit the console and having to retype
“ruby script/console” and wait for it to reload?

Think of it this way… when you start console you are in one session.
Just like if you change a model in the middle of making a request via
your
browser, it’s not going to update. In other words, every instance of
console is a “browser hit”.

You can type “reload!” and it will reload all the classes. This could
invalidate some of your previously find’ed objects though so you might
have to re-find them.

-philip

Thanks you all gentlemen for your helpful comments. I am going to
enjoy being a part of RoR community.
Regards,
Bharat

Nathan L. wrote:

Bharat,

You can reload a class by typing

load ‘classname.rb’

in the console…

Nathan

In development mode all the classes in your app get reloaded on every
request. In the console, there is no request, so there is no reload.

You can however, simply type:

reload!

And your models and everything else should get refreshed.

Nathan L. wrote:

Bharat,

You can reload a class by typing

load ‘classname.rb’

in the console…

Nathan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs