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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:56:23+00:00 2026-06-05T09:56:23+00:00

I have a Forum model which has many discussions. Each discussion has many posts.

  • 0

I have a Forum model which has many discussions. Each discussion has many posts. Discussion is more or less just a collection of post objects.

Now I’d like to have a counter_cache available in Forum for the posts contained in that forums discussions.

So really, it seems like I should be using a :through association instead of two separate associations to accomplish this.

However I can’t find any reference that suggest mixing a has_many :through and has_one :through association, just for one-to-one and many-to-many, not for one-to-many.

class Forum < ActiveRecord::Base
  has_many :discussions
  has_many :posts, :through => :discussions
end

class Post < ActiveRecord::Base
  belongs_to :discussion
  has_one :forum, :through => discussion
end

class Discussion < ActiveRecord::Base
  has_many :posts
  belongs_to :forum
end

Is something like the above advisable, or should I just handle the counter cache manually?

  • 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-05T09:56:25+00:00Added an answer on June 5, 2026 at 9:56 am

    So the normal counter cache seems to be working just fine, as does mixing the relationship types

    class Post < ActiveRecord::Base
      belongs_to :user
      belongs_to :discussion
      has_one :forum, :through => :discussion,
                :counter_cache => :posts_counter
    end
    class Forum < ActiveRecord::Base
      has_many :discussions
      has_many :posts, :through => :discussions
    end
    
    class Discussion < ActiveRecord::Base
      has_many :posts
      belongs_to :forum
      belongs_to :user
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Posts model, which has many posts in many languages . It's
I have question model which has many options. In my question controller new action
I have a Content model which has one or many Audio files which need
I have a model class 'Market' which has many products: class Market < ActiveRecord::Base
I have a notes model, which has a many-to-many relationship with the users model.
I have a model Journey which has many Users (drivers). I want to be
I have a 'Business' model which has_many 'Hours'. Each Hour record has a start_time
So I have a self-joining model defined. Basically a post on a forum, and
I have a Thread model which has a show page, and in that action's
I have a Model Category which has name:string categoryId:integer parent_id:integer the categoryId doesnt matter

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.