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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:08:35+00:00 2026-05-14T04:08:35+00:00

A Post belongs_to a User, and a User has_many Posts. A Post also belongs_to

  • 0

A Post belongs_to a User, and a User has_many Posts.
A Post also belongs_to a Topic, and a Topic has_many Posts.

class User < ActiveRecord::Base
  has_many :posts
end

class Topic < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
   belongs_to :user
   belongs_to :topic
end

Well, that’s pretty simple and very easy to set up, but when I display a Topic, I not only want all of the Posts for that Topic, but also the user_name and the user_photo of the User that made that Post. However, those attributes are stored in the User model and not tied to the Topic. So how would I go about setting that up?

Maybe it can already be called since the Post model has two foreign keys, one for the User and one for the Topic?

Or, maybe this is some sort of “one-way” has_many through assiociation. Like the Post would be the join model, and a Topic would has_many :users, :through => :posts. But the reverse of this is not true. Like a User does NOT has_many :topics.

So would this even need to be has_many :though association? I guess I’m just a little confused on what the controller would look like to call both the Post and the User of that Post for a give Topic.

Edit: Seriously, thank you to all that weighed in. I chose tal’s answer because I used his code for my controller; however, I could have just as easily chosen either j.’s or tim’s instead. Thank you both as well. This was so damn simple to implement. I think today might mark the beginning of my love affair with rails.

  • 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-14T04:08:35+00:00Added an answer on May 14, 2026 at 4:08 am

    Well, if what you want is display the user name of the author of a post, for example, you can do something like (not tested, but should work) :

    @posts = topic.posts.all(:include => :user) 
    

    it should generate one request for all posts, and one for users, and the posts collection should have users.

    in a view (haml here) :

    - @posts.each do |post|
      = post.user.name
      = post.body
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Post model: class Post < ActiveRecord::Base belongs_to :user has_many :taggings, :dependent
Given User: class User < ActiveRecord::Base has_many :discussions has_many :posts end Discussions: class Discussion
class Followup < ActiveRecord::Base belongs_to :post belongs_to :comment end This model needs to only
I have the following models: Post.rb class Post < ActiveRecord::Base belongs_to :category has_many :attachments,
I have my models setup like so: class User < ActiveRecord::Base has_many :posts, :foreign_key
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
Post :belongs_to :user User :has_many :posts In my signup workflow they draft a Post
I'm trying to add comments to a Post model class Comment < ActiveRecord::Base belongs_to
I have the following models. class Post < ActiveRecord::Base has_many :timeline_items, :as=>:subject define_index do
I have a Post model: class Post < ActiveRecord::Base attr_accessible :title, :content, :tag_names belongs_to

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.