假设我们有一个用户。
一个用户通过这样的帐户拥有_many文档......</p>
class User < ActiveRecord::Base
belongs_to :account
has_many :documents, :through => :account, :order => "created_at DESC"
end
class Account < ActiveRecord::Base
has_one :owner, :class_name => "User", :dependent => :destroy
has_many :documents, :dependent => :destroy
end
class Document < ActiveRecord::Base
belongs_to :account
end
又好又简单,这就是棘手的地方……</p>
用户还可以在文档上进行协作,这通过协作者连接表......</p>
class Collaborator < ActiveRecord::Base
belongs_to :user
belongs_to :documnet
end
class Document < ActiveRecord::Base
has_many :collaborators, :dependent => :destroy
has_many :users, :through => :collaborators
accepts_nested_attributes_for :collaborators, :allow_destroy => true
end
这是我不确定的最终用户位。我想添加另一个有很多文档,当您调用 user.documents 时,它会通过他们的帐户和他们正在协作的文档混合这两个文档......</p>
class User < ActiveRecord::Base
belongs_to :account
has_many :documents, :through => :account, :order => "created_at DESC"
#documents need to do both has manys…
has_many :collaborators, :dependent => :destroy
has_many :documents, :through => :collaborators
end
谢谢,它有点长,但我能想到一个巧妙的解决方案。任何帮助将非常感激。