简体   繁体   English

如何以两种不同的方式定义两个相互关联的模型之间的ActiveRecord关系?

[英]How do I define ActiveRecord relationships between two models that relate to each other in two different ways?

In my app I have the classes User, Video, and Vote. 在我的应用程序中,我有类User,Video和Vote。 Users and Videos can relate to each other in two different ways: as a one-to-many or as a many-to-many. 用户和视频可以通过两种不同的方式相互关联:作为一对多或多对多。 The former is when a User submits a Video (one user can submit many videos). 前者是用户提交视频(一个用户可以提交许多视频)。 The latter is when a user votes on a video (users have many videos through votes, and vice versa). 后者是用户对视频进行投票时(用户通过投票获得许多视频,反之亦然)。 Here is my code, which does not work (I think -- I may be doing something wrong in the view). 这是我的代码,它不起作用(我想 - 我可能在视图中做错了)。 Please help me understand the correct way to structure these associations: 请帮助我理解构建这些关联的正确方法:

class User < ActiveRecord::Base
  has_many :videos, :as => :submissions
  has_many :votes #have tried it without this
  has_many :videos, :as => :likes,  :through => :votes
end

class Vote < ActiveRecord::Base
  belongs_to :video
  belongs_to :user
end

class Video < ActiveRecord::Base
  belongs_to :user
  has_many :votes #have tried it without this . . . superfluous?
  has_many :users, :as => :voters, :through => :votes
end

I haven't gone and checked, but it goes something like this: 我没有去检查,但它是这样的:

Instead of 代替

has_many :videos, :as => :likes, :through => :votes

Use 采用

has_many :likes, :class_name => "Video", :through => :votes

Same with the bottom: 与底部相同:

has_many :users, :as => :voters, :through => :votes

becomes

has_many :voters, :class_name => "User", :through => :votes

:as is used for polymorphic associations. :as用于多态关联。 See this chapter in docs for more info. 有关详细信息,请参阅文档中的本章

class User < ActiveRecord::Base
  has_many :videos # Submitted videos
  has_many :votes
  has_many :voted_videos, :through => :votes # User may vote down a vid, so it's not right to call 'likes'
end

class Vote < ActiveRecord::Base
  belongs_to :video
  belongs_to :user
end

class Video < ActiveRecord::Base
  belongs_to :user
  has_many :votes
  has_many :voters, :through => :votes
end

More details can be found here: http://guides.rubyonrails.org/association_basics.html 更多细节可以在这里找到: http//guides.rubyonrails.org/association_basics.html

Hope it helps =) 希望它有帮助=)

Thanks for your help guys, definitely pointed me in the right direction. 谢谢你们的帮助,绝对指出了我正确的方向。 Here is the working code: 这是工作代码:

class User < ActiveRecord::Base
  has_many :videos, :as => :submissions
  has_many :votes
  has_many :likes, :source => :video, :through => :votes 
end

class Vote < ActiveRecord::Base
  belongs_to :video
  belongs_to :user
end

class Video < ActiveRecord::Base
  belongs_to :user
  has_many :votes
  has_many :voters, :source => :user, :through => :votes 
end

PS I kept it as :likes because in this app they won't be able to downvote, only upvote. PS我把它保持为:喜欢因为在这个应用程序中他们将无法进行投票,只能投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 activerecord 以两种不同的方式关联两个模型? - How can I relate two models in two different ways through activerecord? 在Rails中,如何为两个以不同方式相互引用的模型建立ActiveRecord关联? - In Rails, how can I make ActiveRecord associations for two models that reference each other in different ways? 我应该如何在Rails 5中定义相同两个模型之间的多个关系 - How should I define multiple relationships between the same two models in Rails 5 我如何在两个模型之间做两个has_many / belongs_to关系? - How do I do two has_many/belongs_to relationships between two models? 为相同模型之间的两个不同关系建模 - Modelling two different relationships between the same models ActiveRecord同一模型之间的两个不同关联 - Activerecord two different associations between same models Rails ActiveRecord:相同模型之间的两个has_one关系 - Rails ActiveRecord: two has_one relationships between the same models Rails:两个模型之间的两种不同的多对多关系 - Rails: Two different many to many relationships between two models 在ActiveRecord中使用first_or_create的两种不同方式之间的差异 - Difference between two different ways of using first_or_create in ActiveRecord 以不同方式关联两个模型 - Associating two models in different ways
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM