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 `----
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 -- Posted via http://www.ruby-forum.com/.
joey__ <cardologist@gmail.com> writes:> 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? > > You can create a basic_model.rb then in all your other models do: > Model < BasicModel > > If you are getting errors about basic_model.rb not being found, add this > line: > 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 `----
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/HowToAliasColumnNames - james On 2/21/06, efuzzyone@netscape.net <efuzzyone@netscape.net> wrote:> joey__ <cardologist@gmail.com> writes: > > > 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? > > > > You can create a basic_model.rb then in all your other models do: > > Model < BasicModel > > > > If you are getting errors about basic_model.rb not being found, add this > > line: > > 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 > `---- > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- * J * ~
efuzzyone@netscape.net
2006-Feb-22 09:12 UTC
[Rails] Re: Models, Helpers, Modules, etc. - Thanks
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:> 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/HowToAliasColumnNames > > - james > > On 2/21/06, efuzzyone@netscape.net <efuzzyone@netscape.net> wrote: >> joey__ <cardologist@gmail.com> writes: >> >> > 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? >> > >> > You can create a basic_model.rb then in all your other models do: >> > Model < BasicModel >> > >> > If you are getting errors about basic_model.rb not being found, add this >> > line: >> > 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 >> `---- >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > -- > * J * > ~