Forum: Ruby on Rails Models, Helpers, Modules, etc.

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.
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 unknown (Guest)
on 2006-02-21 11:27
(Received via mailing list)
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.

Thanks for your help.
--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| "All animals are equal, but some animals are more equal than others."
|     -- Orwell, Animal Farm, 1945
`----
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-02-21 11:51
unknown wrote:
> 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

Hope this helps,
Joey
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 unknown (Guest)
on 2006-02-21 12:12
(Received via mailing list)
joey__ <cardologist@gmail.com> writes:

>> import the file into the models. Should I do this using modules, and
> model :basic_model
> to application_controller.rb
>
When I tried to do something like the following, I get
#42S02Table 'BasicModel' doesn't exist: SHOW FIELDS FROM BasicModel

class BasicModel <  ActiveRecord::Base
end

class Model < BasicModel
end

Thanks.
--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| "All animals are equal, but some animals are more equal than others."
|     -- Orwell, Animal Farm, 1945
`----
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-02-21 12:46
(Received via mailing list)
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.

For your last question:
http://wiki.rubyonrails.org/rails/pages/HowToAlias...

- james

On 2/21/06, efuzzyone@netscape.net <efuzzyone@netscape.net> wrote:
> >> 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
> `----
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
* J *
  ~
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 unknown (Guest)
on 2006-02-22 10:13
(Received via mailing list)
Hello James,
 Thanks for your answers.

Regards.
--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read the latest news at: http://news.kreeti.com
,----
| "All animals are equal, but some animals are more equal than others."
|     -- Orwell, Animal Farm, 1945
`----

"James Adam" <james.adam@gmail.com> writes:
This topic is locked and can not be replied to.