Aaron Dufall
Aaron Dufall

Reputation: 1177

belongs_to won't associate records even with the correct Id stored

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: "[email protected]", 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

Upvotes: 0

Views: 63

Answers (1)

Anthony Alberto
Anthony Alberto

Reputation: 10405

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

Upvotes: 1

Related Questions