Rails: has_many through with polymorphic association - will this work?

Viewed 14066

A Person can have many Events and each Event can have one polymorphic Eventable record. How do I specify the relationship between the Person and the Eventable record?

Here are the models I have:

class Event < ActiveRecord::Base
  belongs_to :person
  belongs_to :eventable, :polymorphic => true
end

class Meal < ActiveRecord::Base
  has_one :event, :as => eventable
end

class Workout < ActiveRecord::Base
  has_one :event, :as => eventable
end

The main question concerns the Person class:

class Person < ActiveRecord::Base
  has_many :events
  has_many :eventables, :through => :events  # is this correct???
end

Do I say has_many :eventables, :through => :events like I did above?

Or do I have to spell them all out like so:

has_many :meals, :through => :events
has_many :workouts, :through => :events

If you see an easier way to accomplish what I'm after, I'm all ears! :-)

2 Answers
Related