简体   繁体   English

具有相同模型的Ruby on Rails 4的多态关联

[英]Polymorphic Association with same model Ruby on Rails 4

So I have an app in which users can create cars. 因此,我有一个应用程序,用户可以在其中创建汽车。 They can also like cars and I want to create an association between both. 他们也可以喜欢汽车,而我想两者之间建立联系。 Cars that they created belong to them and Cars that they have liked belong to them through the context of liking them. 他们创造的汽车属于他们,而喜欢的汽车则通过喜欢它们的上下文属于他们。 To do this I have set up my associations as follows: 为此,我建立了如下关联:

User Association: 用户协会:

class User < ActiveRecord::Base
  has_many :cars
  has_many :cars, -> {distinct}, through: :likes
end

Car Association: 汽车协会:

class Car < ActiveRecord::Base
  belongs_to :users
  has_many :likes
  has_many :users, -> { distinct }, through: :likes
 end

Like Association: 像协会:

class Like < ActiveRecord::Base
 belongs_to :user
 belongs_to :car
end

The problem is that before I had my user has_many cars through like relationship declared. 问题是,在我让用户has_many汽车通过类似的关系声明之前。 I used to be able to call @user.cars and it would present the user's cars. 我曾经能够调用@ user.cars,它将显示用户的汽车。 Now it returns the collection of the cars the user has liked. 现在,它返回用户喜欢的汽车的集合。 I need methods for each collection. 我需要每个集合的方法。

When I try: User.likes.cars 当我尝试时: User.likes.cars

I get a 我得到一个

No Method error 没有方法错误

and the console log looks through the likes records and still doesn't return the cars even though my likes records have a car_id field. 并且控制台日志会浏览“喜欢”记录,即使我的“喜欢”记录中有一个car_id字段,它也不会返回汽车。

I have looked at a bunch of questions but have trouble understanding them. 我看过很多问题,但难以理解。 I've also tried to define methods in the model and nothing is seeming to work. 我也尝试过在模型中定义方法,但似乎没有任何效果。 Any help is appreciated. 任何帮助表示赞赏。

How would I be able to change my associations so I can have a for query both User.cars (for cars the user has created) and User.likes.cars (for cars the user has liked)? 我将如何更改关联,以便对User.cars(用于用户创建的汽车)和User.likes.cars(对于用户喜欢的汽车)进行查询?

So the below answer from Oleg didn't work exactly but led me in the right direction. 因此,奥列格(Oleg)的以下回答无法完全发挥作用,但却引导我朝着正确的方向前进。 Thank you! 谢谢! I started by following the above example and doing: 我首先按照上述示例进行操作:

    class User < ActiveRecord::Base
       has_many :cars
       has_many :car_likes, -> {distinct}, class_name: 'Car', through: :likes
     end

     class Car < ActiveRecord::Base
       belongs_to :users
       has_many :likes
       has_many :user_likes, -> { distinct }, class_name: 'User', through: :likes
      end

This returned the following error in console: 这在控制台中返回以下错误:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) "car_likes" or :car_like in model Like. ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:在模型Like中找不到源关联“ car_likes”或:car_like。 Try 'has_many :car_likes, :through => :likes, :source => '. 尝试'has_many:car_likes,:through =>:likes,:source =>'。 Is it one of user or car? 是用户还是汽车之一?

So I changed it to: 所以我将其更改为:

class User < ActiveRecord::Base
  has_many :cars
  has_many :car_likes, -> {distinct}, through: :likes, source: :cars
end
Car Association:

class Car < ActiveRecord::Base
  belongs_to :users
  has_many :likes
  has_many :user_likes, -> { distinct }, through: :likes, source: :users
 end

It nows works for both models! 现在适用于两种型号! Thanks and hopefully this is helpful to someone else with the same problem. 谢谢,希望对其他有相同问题的人有所帮助。

has_many :cars, -> {distinct}, through: :likes overrides has_many :cars because it redefines User.cars . has_many :cars, -> {distinct}, through: :likes覆盖has_many :cars因为它重新定义了User.cars Try the following: 请尝试以下操作:

class User < ActiveRecord::Base
  has_many :cars
  has_many :car_likes, -> {distinct}, class_name: 'Car', through: :likes
end
Car Association:

class Car < ActiveRecord::Base
  belongs_to :users
  has_many :likes
  has_many :user_likes, -> { distinct }, class_name: 'User', through: :likes
 end

#To get them, instead of user.likes.cars
@user.car_likes
@car.user_likes

Please let me know if the problem persists. 如果问题仍然存在,请告诉我。 There might be another error. 可能还有另一个错误。

I don't see where you are defining any model as polymorphic. 我看不到您在哪里将任何模型定义为多态的。

In the past I have done something like this.. actually I did this for tags/taggings and made "like" a tag a user applied to another instance. 过去,我做过这样的事情:实际上,我是为标签/标签做的,并让用户“喜欢”一个标签,将其应用于另一个实例。 This is an ad-hoc modification and I may have missed something but it's a pretty common use case for polymorphic associations. 这是一个临时修改,我可能错过了一些东西,但这是多态关联的一个非常普遍的用例。

class Like < ActiveRecord::Base
  belongs_to :likeable, polymorphic: true
  ...
end

class Liking < ActiveRecord::Base
  belongs_to :like
  belongs_to :likeable, :polymorphic => true
end

class User < ActiveRecord::Base
  has_many :likings, :as => :likeable
  has_many :likes,  -> { order(created_at: :desc) }, :through => :taggings
end

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM