Reload the lib directory

Im writing a module in the lib directory.
If I change the module, the changes are not visible to the application
when i releod the web page (i am in the development evironment).
I must restart the web server (WEBrick) and so i see the changes… but
I lost more time.

How can I reload the module in the dirictory lib, without reload
WEBrick?

Thanks so much
–Reis

In one of your files that do get reloaded (say the controller you’re
working with) you can use a ‘load’ statement, like

load ‘my_lib_file.rb’

A better answer, but maybe not immediately helpful: write unit tests
to ensure the code in your lib file works. If you do test-based
development, you don’t need to reload your lib constantly to see
changes in the browser.

cheers
Gerret

Gerret A. wrote:

In one of your files that do get reloaded (say the controller you’re
working with) you can use a ‘load’ statement, like

load ‘my_lib_file.rb’

Thanks… it works, but it is not elegant as Ryby, because i must insert
the load directive in all my controller.
I’m serching the way for automatic loading the modules as rails do for
the helpers. I need this for the the development evironment.

Thanks so much for the answer.
–Reis

Maybe in application controller:

before_filter :load_my_lib

def load_my_lib
load ‘my_lib.rb’
end

2006/1/29, Reis [email protected]:

On 1/29/06, Reis [email protected] wrote:

You can do “require_dependency” instead of “require”, in your
environment.rb file.

Wilson B. wrote:

On 1/29/06, Reis [email protected] wrote:

You can do “require_dependency” instead of “require”, in your
environment.rb file.

This is the solution I was searched… thanks so much to all.
But the require_dependency must be setting in application_controller.rb,
because in the environment.rb file the require and require_dependency do
the same thing.

Thanks
–Reis

Reis wrote:

Wilson B. wrote:

On 1/29/06, Reis [email protected] wrote:

You can do “require_dependency” instead of “require”, in your
environment.rb file.

This is the solution I was searched… thanks so much to all.
But the require_dependency must be setting in application_controller.rb,
because in the environment.rb file the require and require_dependency do
the same thing.

Thanks
–Reis

A last question…
Can I load all the module under a directory (only the first level)
So if i have the dir lib with the module mod1.rb and mod2.rb, i can load
them with a single command.

Thanks
–Reis

Hi Folks,

I’m experiencing some puzzling behavior with a HABTM association. I
have
two models defined as follows:

class Incident < ActiveRecord::Base
has_and_belongs_to_many :conditions
end

Class Condition < ActiveRecord::Base
has_and_belongs_to_many :incidents
End

When I run the following code (irb) the new Condition is not persisted
to
the DB until I call Incident#save. This is what I expect and is not the
problem.

i = Incident.new
i.conditions << Condition.new(:name => ‘some condition’)

Now, my problem is when I add another condition to the saved incident.
Like
this…

i.conditions << Condition.new(:name => ‘some other condition’)

At this point I would not expect to see the new condition persisted to
the
conditions DB table, but I do.

Why is that? Shouldn’t the new condition be persisted to the DB only
after
I call i.save?

Sorry, first code block should read …

i = Incident.new
i.conditions << Condition.new(:name => ‘some condition’)
i.save

I’m not an expert, just a newbie

What if you try

class Incident < ActiveRecord::Base
has_and_belongs_to_many :condition
end

Class Condition < ActiveRecord::Base
has_and_belongs_to_many :incident
End

Instead of

class Incident < ActiveRecord::Base
has_and_belongs_to_many :conditions
end

Class Condition < ActiveRecord::Base
has_and_belongs_to_many :incidents
End

-----Mensaje original-----
De: [email protected]
[mailto:[email protected]] En nombre de Vince P.
Enviado el: domingo, 29 de enero de 2006 13:53
Para: [email protected]
Asunto: [Rails] HABTM weirdness

Hi Folks,

I’m experiencing some puzzling behavior with a HABTM association. I
have
two models defined as follows:

class Incident < ActiveRecord::Base
has_and_belongs_to_many :conditions
end

Class Condition < ActiveRecord::Base
has_and_belongs_to_many :incidents
End

When I run the following code (irb) the new Condition is not persisted
to
the DB until I call Incident#save. This is what I expect and is not the
problem.

i = Incident.new
i.conditions << Condition.new(:name => ‘some condition’)

Now, my problem is when I add another condition to the saved incident.
Like
this…

i.conditions << Condition.new(:name => ‘some other condition’)

At this point I would not expect to see the new condition persisted to
the
conditions DB table, but I do.

Why is that? Shouldn’t the new condition be persisted to the DB only
after
I call i.save?

On 1/29/06 11:22 AM, “Reis” [email protected] wrote:

Reis wrote:

Wilson B. wrote:

On 1/29/06, Reis [email protected] wrote:

You can do “require_dependency” instead of “require”, in your
environment.rb file.

This is the solution I was searched… thanks so much to all.
But the require_dependency must be setting in
application_controller.rb,
because in the environment.rb file the require and require_dependency
do
the same thing.

Thanks
–Reis

A last question…
Can I load all the module under a directory (only the first level)
So if i have the dir lib with the module mod1.rb and mod2.rb, i can
load
them with a single command.

Thanks
–Reis


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

That would change the collection name from Incident#conditions (plural
and
IMHO, more intuitive) to Incident#condition (singular). It does not
address
the problem.

Reis, this should work

Dir[File.join(RAILS_ROOT, ‘lib’, “*.rb”)].each do |file|
require_dependency file
end

cheers
Gerret

Vince,

You are experiencing the normal behaviour of the associations. From the
RDOCs:

Adding an object to a collection (has_many or

has_and_belongs_to_many) automatically saves that object, except if
the parent object (the owner of the collection) is not yet stored in
the database.

If saving any of the objects being added to a collection (via push

or similar) fails, then push returns false.

You can add an object to a collection without automatically saving

it by using the collection.build method (documented below).

All unsaved (new_record? == true) members of the collection are

automatically saved when the parent is saved.

You can read the rest of the documentation on associations at

Cody

On 1/29/06, Vince P. [email protected] wrote:

has_and_belongs_to_many :incidents

this…

So if i have the dir lib with the module mod1.rb and mod2.rb, i can load


Cody F.
http://www.codyfauser.com

Thanks Cody. That did the trick.

Note to self: RTFM!!!

I’m having the same problem of not-reloading; not with modules, but just
with any class that doesn’t inherit from a Rails class. I solved it by
inserting ‘include Reloadable’ in those classes. But I kind of dislike
doing this: Rails should automatically reload everthing in the
development environment, don’t you think so?

:regards => Fino

Gerret A. wrote:

Reis, this should work

Dir[File.join(RAILS_ROOT, ‘lib’, “*.rb”)].each do |file|
require_dependency file
end

cheers
Gerret

Perfect… this is the the solution i need.
Thanks so much Garret!

–Reis