views:

56

answers:

1

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
A: 

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'
mark
I'm confused--what requirements of mine don't match the example I've given? I'm not saying that they do necessarily, just that I'm not understanding what you mean. I'll edit the post to clarify myself a bit--hope that helps.
Brennon Bortz
Hi Brennon. I've edited my answer. Can I ask, what is a layout? Reason I ask is that layout sounds kinda like view (html etc).
mark
That's dead on...thanks, Mark. Layout is a confusing class name, I know. It might be best to rename it, as it is not related to the Rails idea of layouts/views at all. This Rails app will ultimately produce widgets (i.e., iGoogle, Mac Dashboard, Opera Widgets, etc.), and these classes will be used to produce the specifications for several different general types (layouts) of widgets. Thus, they're not related internally to the Rails app at all. really should be more descriptive there, I guess. Template won't work either, I guess! :)
Brennon Bortz
Hey Brennon, you're welcome and glad it's working. Maybe framework would be a better name. Good luck with your app. :)
mark
Same issue, new problem. It seems that since the subclassed layouts don't subclass directly from ActiveRecord::Base, they look to the DefaultLayouts table in the DB for their attributes--not to their own. Any workaround for this?
Brennon Bortz
Have a read of this and tell me what you think. http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html
mark