Sql 复杂,更名有很多通过关系
我正在尝试创建一个Wiki风格的应用程序。现在的基本模型是用户和Wiki。一个用户Sql 复杂,更名有很多通过关系,sql,ruby-on-rails,ruby-on-rails-3,activerecord,ruby-on-rails-4,Sql,Ruby On Rails,Ruby On Rails 3,Activerecord,Ruby On Rails 4,我正在尝试创建一个Wiki风格的应用程序。现在的基本模型是用户和Wiki。一个用户有许多她创建的维基,而一个维基属于一个用户。到目前为止很容易。现在是并发症 我希望Wiki拥有无限的协作用户。这就区分了Wiki的所有者和合作者,尽管两者都是用户。我认为这将是一个很好的例子,一个有很多通过关系,所以我创建了一个联接表:协作 这是我的迁移生成器(我没有偏离)。第一个维基: rails g model Wiki body:text title:string owner_id:integer 然后用户
有许多她创建的维基,而一个维基属于一个用户。到目前为止很容易。现在是并发症
我希望Wiki拥有无限的协作用户。这就区分了Wiki的所有者和合作者,尽管两者都是用户。我认为这将是一个很好的例子,一个有很多通过关系,所以我创建了一个联接表:协作
这是我的迁移生成器(我没有偏离)。第一个维基:
rails g model Wiki body:text title:string owner_id:integer
然后用户:
rails g model User name:string
然后是协作:
rails g model Collaboration wiki_id:integer collaborator_id:integer
我的想法是,我应该能够调用wiki.owner
和wiki.collaborators
,以及user.owned\u wiki
和user.collaborated\u wiki
。我通过硬编码自己的方法实现了大部分的目标。也就是说,这非常有效:
class User < ActiveRecord::Base
def owned_wikis
Wiki.where(owner_id: id)
end
def collaborations
Collaboration.where(collaborator_id: id)
end
def collaborated_wikis
collaborations.wikis
end
end
class Collaboration < ActiveRecord::Base
def self.collaborators
User.where(id: pluck(:collaborator_id))
end
def self.wikis
Wiki.where(id: pluck(:wiki_id))
end
def wiki
Wiki.where(id: wiki_id)
end
def collaborator
User.where(id: collaborator_id)
end
end
class Wiki < ActiveRecord::Base
def owner
User.where(id: owner_id)
end
def collaborations
Collaboration.where(wiki_id: id)
end
def collaborators
collaborations.collaborators
end
end
和u.cooperative_wiki
:
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :collaborated_wiki or :collaborated_wikis in model Collaboration. Try 'has_many :collaborated_wikis, :through => :collaborations, :source => <name>'. Is it one of :wiki or :collaborator?
ActiveRecord::HasManyThroughSourceAssociationNotFoundError:在模型协作中找不到源关联:协作的\u wiki或:协作的\u wiki。试试“有很多:协作的wiki,:通过=>:协作,:source=>”。它是:wiki还是:collaborator?
有什么办法可以做得更好吗?我希望有一个完全成熟的关系,这样我就可以做wiki.collaborators提供:source选项
has_many :collaborated_wikis, through: :collaborations, source: :wiki
has_many :collaborators, through: :collaborations, source: :colaborator
不过,我只想在协作模型上将colaborator_id更改为user_id,因为您在这个模型本身上已经有了一个“协作”名称空间,这将允许您更多地使用rails默认值。Awesome。这完全解决了问题。对于那些感兴趣的人,我将把我的答案放在上面这个答案建议的最终版本中。我可能最终接受了合作者id
=>用户id
的建议,但如果我用命名古怪的外键为自己把事情复杂化,知道如何做是件好事。
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :collaborated_wiki or :collaborated_wikis in model Collaboration. Try 'has_many :collaborated_wikis, :through => :collaborations, :source => <name>'. Is it one of :wiki or :collaborator?
class User < ActiveRecord::Base
has_many :owned_wikis, class_name: 'Wiki', foreign_key: 'owner_id'
has_many :collaborations, foreign_key: 'collaborator_id'
has_many :collaborated_wikis, through: :collaborations, source: :wiki
end
class Collaboration < ActiveRecord::Base
belongs_to :wiki
belongs_to :collaborator, class_name: 'User'
end
class Wiki < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :collaborations
has_many :collaborators, through: :collaborations, source: :collaborator
end
has_many :collaborated_wikis, through: :collaborations, source: :wiki
has_many :collaborators, through: :collaborations, source: :colaborator