Search code examples
ruby-on-railsruby-on-rails-3associationspolymorphic-associationssingle-table-inheritance

Is a polymorphic association appropriate here?


Here's what I'm thinking:

class Widget < ActiveRecord::Base
  has_one :widget_layout
end

class WidgetLayout < ActiveRecord::Base
  belongs_to :widget
  belongs_to :layoutable, :polymorphic => true
end

class InformationalLayout < WidgetLayout
  has_one :widget_layout, :as => :layoutable
end

class OneWayCommunicationLayout < WidgetLayout
  has_one :widget_layout, :as => :layoutable
end

I'm quite sure this is completely wrong. What I'm trying to do seems to be just the reverse of how I've seen polymorphic associations demonstrated. For example, from the Rails Guides:

class Picture < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true
end

class Employee < ActiveRecord::Base 
  has_many :pictures, :as => :imageable 
end 

class Product < ActiveRecord::Base 
  has_many :pictures, :as => :imageable 
end

Here an "owning" model (Product or Employee) can be one of many classes. I'd like to have the "owned" model be one of many classes, all of which inherit base behaviours from another class. In addition, all of these subclasses' behaviour is sufficiently different that STI would be enormously inefficient. I'm quite lost here...I appreciate any help you can give!

EDIT: Just to clarify what I want to do... I have an "owning" class called Widget. A widget represents something along the lines of a very simple web app. Each widget can be represented through one of several different layouts. Hence, I'm defining these different layout classes. The layouts can vary quite a lot, both in look and behaviour, but they do share some common functionality. Because of this, I'd like to extract that common behaviour to the WidgetLayout class from which they can inherit. In the end, any Widget can be associated to one "specific" layout (be it Informational, OneWayCommunication, etc.) I'm just not sure how this setup should be structured.

EDIT (Final one, I hope!): Ahh, looking at your example again I see it. The only thing I'm missing is having the layouts inherit common behaviour. So, would something like this work?:

class Widget < ActiveRecord::Base
  belongs_to :layout, :polymorphic => true
end

class InformationalLayout < WidgetLayout
  has_many :widgets, :as => :layout
end

class OneWayCommunicationLayout < WidgetLayout
  has_many :widgets, :as => :layout
end

class WidgetLayout < ActiveRecord::Base
  # Define common behaviour here
end

Solution

  • Is there a reason why your requirements don't match the example you posted?

    class Widget < ActiveRecord::Base
      belongs_to :layout, :polymorphic => true
    end
    
    class InformationalLayout < DefaultLayout
      has_many :widgets, :as => :layout
    end
    
    class OneWayCommunicationLayout < DefaultLayout
      has_many :widgets, :as => :layout
    end
    
    def DefaultLayout < ActiveRecord::Base
     #no relationship here, only class inheritance 
    
      def get_sorted_data
        return 'example data'
      end
    end
    
    # eg
    widget.find(1).layout.class => InformationalLayout
    widget.find(2).layout.class => OneWayCommunicationLayout
    
    widget.find(1).layout.get_sorted_data => 'example data'
    widget.find(2).layout.get_sorted_data => 'example data'