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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:53:05+00:00 2026-05-14T03:53:05+00:00

I think I’m missing something very obvious and its making my brain hurt. class

  • 0

I think I’m missing something very obvious and its making my brain hurt.

class User < ActiveRecord::Base
has_one :profile

class Profile < ActiveRecord::Base
has_one :user
belongs_to :team

I have a partial that loops through the users and print some basic info, I’m using this partial in my team show page.

I had originally written this to return users who’s profiles were a member of a team.

def show
@team = Team.find_by_id(params[:id])
@profiles= Profile.find(:all, :conditions => ['team_id = ?', @team.id])
@users = User.find_by_id(@profiles.user_id)
end

But quickly realized @profiles was an array, and it looks messy as hell. Stuck as to what my find should look like to select all User who have a profile that is a member of a team.

The partial that is working elsewhere for displaying users looks like this

<% for user in @users%>
<table>
<tr>
        <td>
        <%= image_tag user.profile.picture.url %>
        </td>
        <td>
        <a href="/users/<%= user.id %>"><%= user.login %></a>
        </td>
        <td>
        <%= user.profile.first_name %> <%= user.profile.second_name %>
        </td>
        <td>
        <%= user.profile.status %>
        </td>
</tr>
</table>
<% end %>

Development log output with updated show and relationships

Processing TeamsController#show (for 127.0.0.1 at 2010-03-30 22:06:31) [GET]
  Parameters: {"id"=>"1"}
  User Load (1.3ms)   SELECT * FROM "users" WHERE ("users"."id" = 3) LIMIT 1
  Team Load (1.0ms)   SELECT * FROM "teams" WHERE ("teams"."id" = 1) 
Rendering template within layouts/main
Rendering teams/show
Completed in 75ms (View: 11, DB: 2) | 200 OK [http://localhost/teams/1]
  • 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-14T03:53:05+00:00Added an answer on May 14, 2026 at 3:53 am

    I would change your associations to something like this:

    class User < ActiveRecord::Base
    has_one :profile
    
    class Profile < ActiveRecord::Base
    belongs_to :user
    belongs_to :team
    
    class Team < ActiveRecord::Base
    has_many :profiles
    has_many :users, :through => :profiles
    

    I think the problem you are having is that in a has_one relationship one has to be the owner and one has to be the ownee.. (belongs_to)

    Then you should be able to find associations using the following:

    @user.profile
    @team.users
    @team.profiles
    

    etc..

    If you want the user to have a profile and the team to display its user’s profiles you would change the models to this:

    class User < ActiveRecord::Base
    has_one :profile
    belongs_to :team
    
    class Profile < ActiveRecord::Base
    belongs_to :user
    
    class Team < ActiveRecord::Base
    has_many :users
    has_many :profiles, :through => :users
    

    This way you could load your team, its users and the users profiles:

    @team = Team.find(params[:id])
    @users = @team.users
    @profiles = @team.profiles
    

    Good luck!

    In order for your associations to work properly with the above code your db tables will need to have the following:

    Your profiles table will need to have user_id as an integer.

    Your users table will need to have team_id as an integer.

    Your teams table doesn’t need any ids.

    Verify that your migrations setup these id’s properly and that when a user is created that it is associated with your team properly:

    @team.users << @user
    

    Hope that helps!

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

Sidebar

Related Questions

I think this should be an easy problem to solve, however its turned into
I think the file that is produced is an .asm file, any idea how
I think most people know how to do this via the GUI (right click
I think most people here understand the importance of fully automated builds. The problem
I think this is specific to IE 6.0 but... In JavaScript I add a
I think it can be done by applying the transformation matrix of the scenegraph
I think that java executables (jar files) are trivial to decompile and get the
I think I've got it down to the most basic case: int main(int argc,
I think I know how to handle this case, but I just want to
I think title should be good enough.

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.