【发布时间】:2017-02-01 09:16:20
【问题描述】:
我与偏爱客户的用户建立了多对多关系(通过 favorites.rb),我想知道如何将 CollectionProxy 转换为 AssociationRelation,这样我就可以与所有作为用户的客户建立关系收藏夹。
或者只是简单地说,如何将它们全部放在 AssociationRelation 中 - 不必从 CollectionProxy 中转换。
编辑: 我正在寻找的只是一个列出所有客户的关系。我意识到我提出了不同的问题。 我如何获得与所有客户的关系? - 不是与 favourite_id、user_id 等的关系 - 我想要纯粹与客户及其行的关系。
我可以像这样找到 CollectionProxy:
user = User.last
user.favorites # Gives me a CollectionProxy
但是我如何找到最喜欢的客户作为 AssociationRelation?
型号:
user.rb:
has_many :favorites, :dependent => :destroy
has_many :clients, through: 'favorites'
客户端.rb
has_many :favorites, :dependent => :destroy
has_many :users, through: 'favorites'
Favorite.rb
belongs_to :user
belongs_to :client
【问题讨论】:
标签: ruby-on-rails ruby activerecord rails-activerecord ruby-on-rails-5