2014-06-18 85 views
0

我有一個用戶模型和組模型。Rails has_one和has_many關聯在同一個模型上

用戶has_and_belongs_to_many :groups和組has_and_belongs_to_many :users。 關聯和連接表正常工作。

一個組只能擁有一個擁有該組的用戶,我想要能夠呼叫@group.owner返回擁有該組的用戶,但不確定如何去做,我創建了一個用戶引用列在組表rails g migration add_user_to_groups user:references ,但我不知道要在組模型中放什麼,像has_one :user, as: :owner

這是正確的方法嗎?

回答

2
has_one :owner, class_name: "User", foreign_key: 'user_id' 
+0

它需要是belongs_to工作 –

+0

你的意思是說,'has_one'會給你帶來錯誤? –

+0

是的,它會產生一個錯誤 –

相關問題