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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:16:23+00:00 2026-06-08T12:16:23+00:00

When a User is created a Team is created and the User is set

  • 0

When a User is created a Team is created and the User is set as the owner of that team and also set as a member of that team.

A User can be a member of many Teams and Teams can have many members.
Each User can own only one team and a Team can have only owner.

now I’m getting some strange behavior, that I can figure out

1.9.3p194 :001 > user = User.first
User Load (0.1ms)  SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, email: "test@example.com", encrypted_password: "$2a$10$KhQ7z3Qb6QXdsBA/MmPLuecrbIV4cGEaMGhIepLjuzla...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2012-07-28 01:33:31", last_sign_in_at: "2012-07-28 01:33:31", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2012-07-28 01:33:31", updated_at: "2012-07-28 01:33:31"> 
1.9.3p194 :002 > user.teams
Team Load (0.1ms)  SELECT "teams".* FROM "teams" INNER JOIN "teams_users" ON "teams"."id" = "teams_users"."team_id" WHERE "teams_users"."user_id" = 1
=> [#<Team id: 1, name: "username", dept_no: nil, created_at: "2012-07-28 01:33:31", updated_at: "2012-07-28 01:33:31", user_id: 1>] 
1.9.3p194 :003 > user.owned_team
Team Load (0.2ms)  SELECT "teams".* FROM "teams" WHERE "teams"."user_id" = 1 LIMIT 1
=> #<Team id: 1, name: "username", dept_no: nil, created_at: "2012-07-28 01:33:31", updated_at: "2012-07-28 01:33:31", user_id: 1> 

Up to this point it all works as expected, but when I try to access the association form the other end, the connection isn’t made.

See below I have pulled out the same team record as user.owned_team and user team.owner it returns nil

1.9.3p194 :004 > team = Team.first
Team Load (0.1ms)  SELECT "teams".* FROM "teams" LIMIT 1
=> #<Team id: 1, name: "username", dept_no: nil, created_at: "2012-07-28 01:33:31", updated_at: "2012-07-28 01:33:31", user_id: 1> 
1.9.3p194 :005 > team.owner
=> nil 
1.9.3p194 :006 > user.owned_team == team.owner
=> false 
1.9.3p194 :007 > 

User.rb

class User < ActiveRecord::Base

  has_and_belongs_to_many :teams
  has_one :owned_team, :class_name => "Team"

  after_create :set_user_on_team

  private

  def set_user_on_team
    self.owned_team = self.teams.create(:name => 'username' ) 
  end
end

Team.rb

class Team < ActiveRecord::Base

  attr_accessible :name
  has_and_belongs_to_many :users
  belongs_to :owner, :class_name => "User"

end
  • 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-08T12:16:24+00:00Added an answer on June 8, 2026 at 12:16 pm

    I think you’re missing a foreign key declaration somewhere.

    Say you have a owner_id field in the team table :

    class User < ActiveRecord::Base
      has_one :owned_team, :class_name => "Team", :foreign_key => 'owner_id'
    end
    
    class Team < ActiveRecord::Base
      belongs_to :owner, :class_name => "User", :foreign_key => 'owner_id'
    end
    

    This way rails knows how to pull the data when you access both links

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

Sidebar

Related Questions

I have a user that created about 50 sheets inside a Google Spreadsheet. Each
I have created a user control, the other members of my team simply place
I am using spring mvc in my application,when i created user i have to
I have been working on an iPhone project, where we created all the user
I created a model similar to the following: class Pick(models.Model): user = models.ForeignKey(User) team
I have a User model with devise, a Team model, a Player model and
My team has a template (XDP) that we've created with the Adobe LiveCycle designer.
I have a SSRS report which displays daily status report for the team members.
Our team is currently developing a web application, in that we have a class
I'm building a web application that will allow team collaboration. That is, a user

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.