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 8738485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:44:22+00:00 2026-06-13T10:44:22+00:00

I have the following models: class User < ActiveRecord::Base has_many :subscriptions, as: :subscribable has_many

  • 0

I have the following models:

class User < ActiveRecord::Base
  has_many :subscriptions, as: :subscribable
  has_many :user_to_high_school_subscriptions
  has_many :high_school_subscriptions, through: :user_to_high_school_subscriptions

  def all_subscriptions
    self.subscriptions + self.high_school_subscriptions.subscriptions
  end
end

class UserToHighSchoolSubscription < ActiveRecord::Base
  belongs_to :user
  belongs_to :high_school_subscription
end

class HighSchoolSubscription < ActiveRecord::Base
  has_many :user_to_high_school_subscriptions
  has_many :users, through: :user_to_high_school_subscriptions
  has_many :subscriptions, as: :subscribable
end

class Subscription < ActiveRecord::Base
  belongs_to :subscribable, polymorphic: true
end

Is there a clever way for me to get ALL Subscriptions that a User has.

I tried

u = User.first
subs = u.all_subscriptions

but that is erroring out (undefined method subscriptions' for #<ActiveRecord::Relation:). I think it’s choking when I try to use the has_many :subscriptions on the HighSchoolSubscription because a user has_many :high_school_subscriptions. (This line: self.high_school_subscriptions.subscriptions).

Is there a way to aggregate has_many on a has_many in Rails?

Running rails 3.2.1

  • 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-06-13T10:44:23+00:00Added an answer on June 13, 2026 at 10:44 am

    self.subscriptions does not return an Array but ActiveRecord::Relation. That’s the reason why the + methods and does not work and your get the mentioned error. The simplest fix is to do it this way:

    def all_subscriptions
      self.subscriptions.all + self.high_school_subscriptions.all.collect { |hss| hss.subscriptions.all }.flatten
    end
    

    The all method will trigger the database queries and return an array. Because a user might have many high school subscriptions and these also may has many subscriptions, you have to iterate over all high school subscriptions and collect their subscriptions. As you can see, this is a complete overkill.

    Redesign your data model or just do it a different way.

    Perhaps, scoping the Subscription model might be the way. Add it an attribute that would specify what kind of subscription it is and then you can completely remove the HighSchoolSubscription model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models: class User < ActiveRecord::Base has_many :subscriptions end class Subscription
I have the following three models: class User < ActiveRecord::Base has_many :associations has_many :pharmacies,
I have the following models, with their relevant associations: class User < ActiveRecord::Base has_many
I have the following models class Project < ActiveRecord::Base has_many :project_members has_many :members, :through
I have three Models setup with the following associations class User < ActiveRecord::Base has_many
I have the following models: class User < ActiveRecord::Base has_many :books, :dependent => :destroy
In my models I have the following setup: class User < ActiveRecord::Base has_many :assignments
I have a nested form with the following models: class Incident < ActiveRecord::Base has_many
I currently have the following models: class Player < ActiveRecord::Base belongs_to :team belongs_to :user
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent =>

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.