2010-01-13 34 views

回答

4
class Revision < ActiveRecord::Base 
    belongs_to :creator, :foreign_key => "created_by", :class_name => "User" 
    belongs_to :verifier, :foreign_key => "verified_by", :class_name => "User" 
    belongs_to :publisher, :foreign_key => "published_by", :class_name => "User" 
end 

class User < ActiveRecord::Base 
    has_many :creations, :foreign_key => "created_by", :class_name => "Revision" 
    has_many :verifications, :foreign_key => "verified_by", 
    :class_name => "Revision" 
    has_many :publications, :foreign_key => "published_by", 
    :class_name => "Revision" 
end 

利用這些模型:

User.first.creations #=> [#<Revision>, #<Revision>, ...] 
Revision.first.creator #=> #<User> 
# ...etc., for verifier/verifications and publisher/publications. 

你或許應該更改關聯名稱到任何最有意義的您的應用程序。

+0

非常感謝! – middric 2010-01-13 11:47:54

+0

我在我自己的問題中嘗試這個解決方案,我似乎無法得到它的工作。你願意在這裏幫我一把嗎? http://stackoverflow.com/questions/5554590/two-models-with-multiple-foreign-keys – bruno077 2011-04-05 22:45:54

相關問題