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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:19:19+00:00 2026-05-13T09:19:19+00:00

This is probably a dumb question, but I can’t seem to find a good

  • 0

This is probably a dumb question, but I can’t seem to find a good answer. I want to know the best way to refer back to the model that an object belongs to.

For example:

class User < ActiveRecord::Base
  has_many :posts
end

class Post < ActiveRecord::Base
  belongs_to :users
end

So, to get the user’s posts, I can use user.posts, but to get the post’s user, I cannot do the reverse: post.user

If I add a “user” method to the Post model, it works, but it doesn’t seem to be the best way.

class Post < ActiveRecord::Base
  belongs_to :users

  def user
    User.find(self.user_id)
  end
end

If you look at this blog post http://www.fortytwo.gr/blog/18/9-Essential-Rails-Tips as an example, you can see that the author uses post.user.username, which doesn’t work out of the box as well as :include => [:user], which also doesn’t work, even with the “user” method in the Post model.

I know this is rudimentary, so thanks for your patience. I just want to know the best way to accomplish this relation.

My main goal is to write “finds” using nested includes, which refer back to the user like so:

post = Post.find(:all, :include => [:user])

When I try this, I get “ActiveRecord::ConfigurationError: Association named ‘user’ was not found; perhaps you misspelled it?”

Thanks a lot.

  • 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-13T09:19:19+00:00Added an answer on May 13, 2026 at 9:19 am

    I’m a bit new to Rails, but this should work automatically…

    Ah – you’ve named the parent class in Post as belongs_to :users; but because it only belongs to a single user, Rails is expecting belongs_to :user (or, of course, belongs_to :users, :class_name => "User").

    That is:

    class Post < ActiveRecord::Base
      belongs_to :user
    end
    

    should do the job.

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

Sidebar

Related Questions

Hey this is probably a dumb question but i can't seem to find a
This is probably a dumb question, but I can't find a definitive answer anywhere.
This is probably a dumb question but I can't find it anywhere... Which icon
I know this is probably a dumb question but I can't figure it out
this is probably a dumb question, but I couldnt find the answer I was
This is probably a really dumb question, but I can't seem to figure it
This question is probably quite dumb, but I can't find an example and can't
This is probably a really dumb question with a simple answer but... I am
I know this is probably really dumb, but I can't figure out why this
sorry this is probably a dumb question, but I want to make the background

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.