Hello,
I am learning rails and ruby and I have some questions about how
should I go
about doing certain things.
[1]Many of my models have some functionality which is common, and
instead of
duplicating it across models, I want to store it in a single file, and
then
import the file into the models. Should I do this using modules, and
then store
them in the `lib’ directory? What is the purpose of the tasks
sub-directory in
the lib folder?
There is a similar repetition of functionality among the controllers, so
is it
proper to create a new controller class containing the duplicated
functionality
and then inheriting my other controllers from this class? Or again use
modules
to do so?
[2]How can I provide an alternative name for a column in a model.
alias_method :new_column_name, :old_column_name provides a new name only
for
the reader method, I want this to be done for the writer also.
Hello,
I am learning rails and ruby and I have some questions about how
should I go
about doing certain things.
[1]Many of my models have some functionality which is common, and
instead of
duplicating it across models, I want to store it in a single file, and
then
import the file into the models. Should I do this using modules, and
then store
them in the `lib’ directory? What is the purpose of the tasks
sub-directory in
the lib folder?
There is a similar repetition of functionality among the controllers, so
is it
proper to create a new controller class containing the duplicated
functionality
and then inheriting my other controllers from this class? Or again use
modules
to do so?
[2]How can I provide an alternative name for a column in a model.
alias_method :new_column_name, :old_column_name provides a new name only
for
the reader method, I want this to be done for the writer also.
You can create a basic_model.rb then in all your other models do:
Model < BasicModel
With the controllers put the global stuff in application_controller.rb,
as all controllers inherit from this(unless explicity specified)
If you are getting errors about basic_model.rb not being found, add this
line:
model :basic_model
to application_controller.rb
If you use subclassing with active record, it assumes you want to use
single table inheritance. The right way to go about what you’re
looking for is to put the functionality into a module, and include
that in the classes which need it. The lib directory is a fine place
to put this module.
The tasks folder can hold a ‘.rake’ file in which you can create your
own rake tasks.
duplicating it across models, I want to store it in a single file, and
If you are getting errors about basic_model.rb not being found, add this
class Model < BasicModel
`----