Hello.
I'm wondering how to add methods to a model (stored in /app/models) from a ruby file located in /lib/my-extension for instance. How is it possible? Are the models of an app stored in a specific ruby module?
Thanks.
aurels
Hello.
I'm wondering how to add methods to a model (stored in /app/models) from a ruby file located in /lib/my-extension for instance. How is it possible? Are the models of an app stored in a specific ruby module?
Thanks.
aurels
Aurels wrote:
I'm wondering how to add methods to a model (stored in /app/models) from a ruby file located in /lib/my-extension for instance. How is it possible? Are the models of an app stored in a specific ruby module?
Classes often must extend in the correct order. Maybe the ./app/models version must interpret before the ./lib version. If so, the top of ./lib/my_model.rb should say require RAILS_ROOT + '/app/models/my_model', to get the correct one.
Otherwise, the top of ./app/models/my_model could likewise require the ./lib version.
But this opens the question why not put everything in one model?
Either way, the "magic loader" system (aka "autorequire") will hide your app/model version if any interpretation sees the ./lib version first. The MyModel class name will be satisfied, and not trigger an auto-require. You must require the explicit app/model version at the top of any .rb file that you expect to correctly see it.
Now, why are you trying to do this?
Thanks for your reply
Now, why are you trying to do this?
I want to separate a complicated method outside of the model and to be able to "desactivate" it easily...
More simply, isn't it possible to define a class in two INDEPENDANT files?
e.g.,
file1.rb: class A def m ; end end
file2.rb class A def n; end end
and I want - A to have the method m if only file1.rb was loaded - A to have the method n if only file2.rb was loaded - A to have both m and n if the two files were loaded
I finally found my way out using a module and an include. It's not completely independent but better than putting everything in the same place!