Forum: Ruby on Rails Reload the lib directory

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Reis (Guest)
on 2006-01-29 17:29
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
Gerret A. (Guest)
on 2006-01-29 17:36
(Received via mailing list)
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
Reis (Guest)
on 2006-01-29 17:48
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
Bruno C. (Guest)
on 2006-01-29 18:00
(Received via mailing list)
Maybe in application controller:

before_filter :load_my_lib

def load_my_lib
  load 'my_lib.rb'
end

2006/1/29, Reis <removed_email_address@domain.invalid>:
Wilson B. (Guest)
on 2006-01-29 18:00
(Received via mailing list)
On 1/29/06, Reis <removed_email_address@domain.invalid> wrote:
>
You can do "require_dependency" instead of "require", in your
environment.rb file.
Reis (Guest)
on 2006-01-29 18:13
Wilson B. wrote:
> On 1/29/06, Reis <removed_email_address@domain.invalid> 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 (Guest)
on 2006-01-29 18:22
Reis wrote:
> Wilson B. wrote:
>> On 1/29/06, Reis <removed_email_address@domain.invalid> 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
Vince P. (Guest)
on 2006-01-29 18:54
(Received via mailing list)
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?
Vince P. (Guest)
on 2006-01-29 19:00
(Received via mailing list)
Sorry, first code block should read ...

i = Incident.new
i.conditions << Condition.new(:name => 'some condition')
i.save
Rodrigo D. (Guest)
on 2006-01-29 19:04
(Received via mailing list)
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: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] En nombre de Vince P.
Enviado el: domingo, 29 de enero de 2006 13:53
Para: removed_email_address@domain.invalid
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" <removed_email_address@domain.invalid> wrote:

> Reis wrote:
>> Wilson B. wrote:
>>> On 1/29/06, Reis <removed_email_address@domain.invalid> 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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Vince P. (Guest)
on 2006-01-29 20:04
(Received via mailing list)
That would change the collection name from Incident#conditions (plural
and
IMHO, more intuitive) to Incident#condition (singular).  It does not
address
the problem.
Cody F. (Guest)
on 2006-01-29 20:04
(Received via mailing list)
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
http://api.rubyonrails.org/classes/ActiveRecord/As...



Cody


On 1/29/06, Vince P. <removed_email_address@domain.invalid> 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
Gerret A. (Guest)
on 2006-01-29 20:40
(Received via mailing list)
Reis, this should work

Dir[File.join(RAILS_ROOT, 'lib', "*.rb")].each do |file|
  require_dependency file
end

cheers
Gerret
Reis (Guest)
on 2006-01-29 21:28
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
Vince P. (Guest)
on 2006-01-29 21:59
(Received via mailing list)
Thanks Cody.  That did the trick.

Note to self: RTFM!!!
Fino F. (Guest)
on 2006-07-15 21:37
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
This topic is locked and can not be replied to.