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

  • Home
  • SEARCH
  • 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 6040169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:28:30+00:00 2026-05-23T06:28:30+00:00

What’s the rails way to see posts nested beneath several associative models that a

  • 0

What’s the rails way to see posts nested beneath several associative models that a user habtm?

    @user = current_user
    @user_clubs = @user.clubs   #user is a member of many clubs (habtm), clubs have many events to post a quantity of products to
    @uc_products = @user_clubs.collect {|a| a.products}  # clubs have many products (and categories, haven't implemented yet) (with title, description, etc)
   # @ucp_posts = @uc_categories.collect {|a| a.posts}   # products have many posts (product_id, quantity, & date offered only)

the logger gives me collections, so I know the code is working up till that point

#<User:0x58d4300>
#<Club:0x5aa82e8>#<Club:0x5aa3578>
#<Product:0x59150e8>#<Product:0x5911bc0>#<Product:0x58582b0>

I can collect products, but as soon as I try and collect posts from that, it gives me the error

undefined method `posts' for #<Class:0x5a248d0>

I’ve tried :include, both directions, to no avail.
Edit: here’s most of my models: (I thought it might crowd things before, didn’t include)

        class Post < ActiveRecord::Base
          belongs_to :product, :include => :club
          belongs_to :event

    class Product < ActiveRecord::Base
        belongs_to :user
        belongs_to :club
        belongs_to :category
        has_many :posts

    class Club < ActiveRecord::Base
        has_many :products, :include => :posts
            has_many :events
            belongs_to :users_clubs
            has_many :users_clubs
            has_many :users, :through => :users_clubs, :foreign_key => :users_club_id

class UsersClub < ActiveRecord::Base  #table for joining habtm
    has_many :users
    has_many :clubs
    belongs_to :user
    belongs_to :club

class Event < ActiveRecord::Base
    has_many :posts
    belongs_to :club


    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

      # Setup accessible (or protected) attributes for your model
      attr_accessible :name, :email, :password, :password_confirmation, :remember_me,
                      :bio, :reason, :barter

                      #:email, :name, :bio, :reason, :barter, :

      belongs_to :roles_users                
      has_many :roles_users
      has_many :roles, :through => :roles_users

      belongs_to :users_clubs                
      has_many :users_clubs
      has_many :clubs, :through => :users_clubs, :foreign_key => :users_club_id

      has_many :approvals, :dependent => :destroy
      has_many :products, :dependent => :destroy
      has_many :posts, :dependent => :destroy
      has_many :orders, :dependent => :destroy

My research:
I’ve discovered tree structures, but the structure stays within it’s own model, so that wasn’t fruitful. I’ve looked through http://guides.rubyonrails.org/association_basics.html as well, and could only get as far as I did. I tried looping through the products once, but that gave me the error ‘do’ and ‘end’ were both unexpected. I thought of searching for ‘multiple tags query’ but the results weren’t going 4 levels deep, so that didn’t help much either. I’d include extra columns in my table to make it easy, but I wanted to keep things DRY.

Whatdya think? Or what would be a good search term for me to try on google? Any help is much appreciated for this noob.

Edit 2
Found something here, will test later
Rails Associations Through Multiple Levels (no luck)
How to multi-level Associations? (looks promising)

  • 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-23T06:28:31+00:00Added an answer on May 23, 2026 at 6:28 am

    After weeks of toil on this..

    I’ve found the trick is this gem http://rubygems.org/gems/nested_has_many_through which can do something like this:

    class Author < User
      has_many :posts
      has_many :categories, :through => :posts, :uniq => true
      has_many :similar_posts, :through => :categories, :source => :posts
      has_many :similar_authors, :through => :similar_posts, :source => :author, :uniq => true
      has_many :posts_of_similar_authors, :through => :similar_authors, :source => :posts, :uniq => true
      has_many :commenters, :through => :posts, :uniq => true
    end
    
    class Post < ActiveRecord::Base
      belongs_to :author
      belongs_to :category
      has_many :comments
      has_many :commenters, :through => :comments, :source => :user, :uniq => true
    end
    

    This has super-simplified my queries and collections.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post

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.