Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6250317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:24:06+00:00 2026-05-24T13:24:06+00:00

A Person can have many Events and each Event can have one polymorphic Eventable

  • 0

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! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:24:07+00:00Added an answer on May 24, 2026 at 1:24 pm

    You have to do:

    class Person < ActiveRecord::Base
      has_many :events
      has_many :meals, :through => :events, :source => :eventable,
        :source_type => "Meal"
      has_many :workouts, :through => :events, :source => :eventable,
        :source_type => "Workout"
    end
    

    This will enable you to do this:

    p = Person.find(1)
    
    # get a person's meals
    p.meals.each do |m|
      puts m
    end
    
    # get a person's workouts
    p.workouts.each do |w|
      puts w
    end
    
    # get all types of events for the person
    p.events.each do |e|
      puts e.eventable
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class Person which can have several Homes, each one with one
I have a Person table. Each person can have many names in the PersonNames
Say you have a model Person. Each Person object can have many Friends (Field_HasMany).
I have a person model and a quotes model. Each person can have many
I have table Person. One person can have many siblings from the same table.
Given 2 tables: Person PersonsFavoriteColors A person can have one or more favorite colors.
I have a WPF UserControl representing a Person with many fields. Some Persons can
Let's say I have a table called Event which has a to-many relationship with
I have a table columns:Name,Role,Dept A person can have many roles. So while retrieving
I'm trying to model a network of Person s. A Person can have many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.