2013-07-29 97 views
0

我有一個表有每個用戶的信息。該表被命名爲'user'。 「用戶」表具有「清除」列,該列具有每個用戶的許可級別ID。
然後,我還有另一張名爲'clearances'的表格,它包含每個清除級別的描述。
在問這個問題之前,我環視了一下,發現我可以使用has_many和belongs_to進行關聯,但我真的無法弄清楚如何使用它們。
如何將我'用戶'表中的'清除'列與我的'清除'表相關聯,並使其顯示關於我的清除等級的描述?使用has_many和belongs_to

回答

0
class User < ActiveRecord::Base 
    belongs_to :clearance, :foreign_key => "clearance" 
end 

class Clearance < ActiveRecord::Base 
    has_many :users, :foreign_key => "clearance" 
end 
+0

這是什麼意思:foreign_key和我怎麼稱視圖上的清除說明? –

+0

foreign_key是一個表中唯一標識另一個表的行的字段。如果它遵循軌道命名慣例,那麼您不必明確提及它,這將是「clearance_id」。你可以訪問描述爲'user.clearance.description' – usha

+1

我瞭解了很多關於表格關係的幫助。非常感謝! –