如何在多對多關係中添加所有權?用戶和文件的多對多關係及其在rails中的所有權
例如像這樣的模型。
class User < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :files, through: :editabilities
end
class File < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :users, through: :editabilities
end
class Editabilities < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
我想爲用戶和文件添加一對多關係。
起初我認爲最好是將owner
布爾列添加到Editabilities,但我不知道如何處理它。
其次,我想如果我做一個新的路口模型Ownerships
,那麼我可以像Editabilities
一樣處理。但是當我用這樣的代碼嘗試它時,我得到了一個uninitialized constant User::Ownership
。
class User < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :ownerships, dependent: :destroy
has_many :files, through: :editabilities
has_many :owned_files, through: :ownerships, source: :file
end
class File < ActiveRecord::Base
has_many :editabilities, dependent: :destroy
has_many :ownerships, dependent: :destroy
has_many :users, through: :editabilities
has_one :owner, through: :ownerships, source: :user
end
class Editabilities < ActiveRecord::Base
belongs_to :user
belongs_to :file
end
class Ownerships < ActiveReord::Base
belongs_to :user
belongs_to :file
end
我該如何實現這樣的功能?