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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:44:23+00:00 2026-06-11T21:44:23+00:00

New to rails here. My associations are setup as below, it might not be

  • 0

New to rails here. My associations are setup as below, it might not be the ideal way so any recommendation is greatly welcomed:

User has many posts (posts table has user_id)
User has one profile (profile table has user_id)

Within my post controller view, I want to display the name of the profile (profile.name) that belongs to the user who owns the post.

My naive first guess was <%= @post.user.profile.name %> but that obviously didn’t work.

This is the association defined in my models:

class User < ActiveRecord::Base
  has_many :posts
  has_one :profile
end

class Post < ActiveRecord::Base
  belongs_to :user
end

class Profile < ActiveRecord::Base
  belongs_to :user
end

This is the code in the view: <%= @post.user.profile.name %>

The error is undefined method profile

  • 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-11T21:44:24+00:00Added an answer on June 11, 2026 at 9:44 pm

    you have to mention the following in your models
    user.rb

    has_many :posts
    has_one :profile
    

    profile.rb

    belongs_to :user
    

    post.rb

    belongs_to :user
    

    Then <%= @post.user.profile.name %> should work.

    And if still there is a problem in your data

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

Sidebar

Related Questions

I'm new to Rails and don't grasp yet all the possibilities with associations. Here's
I use the rails 3.0.0.beta Is there any new syntax to write RJS, here
Complete new person to Ruby and Rails here... Have tried some tutorials in the
It's a brand new project. Here's the exact commands I've run: rails new MyProject
I can't figure out why rails has_one :through associations don't accept assignments. Here is
I'm quite new to Rails, so please excuse any wildly inaccurate terminology. First, some
New Rails programmer here. There is probably a pretty simple solution to this, but
i am fairly new to rails and want to take advantage of the associations
Background: I followed the tutorial here to setup a polymorphic User favorites data model
I am reasonably new to Ruby on Rails so I am not sure how

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.