Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails custom foreign_key name on both table

I have two models, for example User and Club with their attributes:

User:
  id
  uid
  email
  etc.

and

Club:
  id
  player_id
  address
  supporter
  etc.

For some reason, the join attribute is clubs.player_id with users.uid NOT clubs.player_id with users.id. Is it possible connecting these two model with one-to-one association using has_one and belongs_to? thx

like image 893
raymondralibi Avatar asked May 17 '12 17:05

raymondralibi


1 Answers

I bet this would work:

class User < ActiveRecord::Base
  has_one :club, :foreign_key => :player_id, :primary_key => :uid
end

class Club < ActiveRecord::Base
  belongs_to :user, :foreign_key => :player_id, :primary_key => :uid
end
like image 53
Adam Avatar answered Nov 10 '22 07:11

Adam