I have to objects with common functionalities and a lot of difference. So I want that each have his own table but inherit the common functionalities from a parent class. How can I do this in Rails ?
I have to objects with common functionalities and a lot of difference. So I want that each have his own table but inherit the common functionalities from a parent class. How can I do this in Rails ?
Polymorphic association might be the way to go...
See the docs for more...
How about just creating your own module and including that in your classes?
# lib/common_methods.rb
module CommonMethods def all_get_this_method ... end end
app/models/company.rb
require 'common_methods' class Company < Active::Record::Base include Commonmethods ... end
config/application.rb
module Yourapp class Application < Rails::Application config.autoload_paths += %W(#{config.root}/lib) ...
Matias Fierro wrote in post #959653:
I have to objects with common functionalities and a lot of difference. So I want that each have his own table but inherit the common functionalities from a parent class. How can I do this in Rails ?
class GenericModel < ActiveRecord::Base self.abstract_class = true
# define all the 'common' methods you want in this class # (including the default behaviors for common methods) # I use methods in this class for providing all my cache # fragment management functionality, rendering model instances # to PDFs, etc, etc end
class Person < GenericModel # person-specific methods, and overrides of common methods # from GenericModel end
class Address < GenericModel # address-specific methods, and overrides of common methods # from GenericModel end
It sounds like you may want to use Multiple Table Inheritance in this case. I was looking to set up something similar a while back and came across this article:
Hope that helps you out!