Interesting Things
- Removing class definitions with Object.send(:remove_const, :Foo)
Sometimes it is necessary to reload or replace a stub/test class during testing (apparently rspec is particularly susceptible to this). This can be achieved using Object.send(:remove_const, :Foo) where Foo is the camel-cased name of the class. If your class is defined within a module, it may be necessary to use Module.send(:remove_const, :Foo)
Ask for help
- foo.bar.reload versus foo.bar(true)
As a point of curiosity, does anybody know when has_many relationship reloading moved from (or to?) foo.bar(true) to foo.bar.reload? The reload syntax seems preferable for readability; will foo.bar(true) continue to work in the future?
Calling `#reload` is just an instance method on ActiveRecord::Base to reload whatever object you call it on from the database. Since the `bar` in `foo.bar` is just an ActiveRecord, you can reload it.
The `foo.bar(true)` syntax is an association proxy specific way of reloading your association.
I think.
October 31, 2008 at 2:01 pm