【发布时间】:2016-07-12 17:58:21
【问题描述】:
我有:
class UserItem < ActiveRecord::Base
belongs_to :user
belongs_to :item
scope :equipped, -> { where(equipped: true) }
end
class Item < ActiveRecord::Base
has_many :user_items
has_many :users, through: :user_items
scope :armor, -> { where(type: 'Armor') }
delegate :equipped, to: :user_items
end
编辑:
如果我尝试
User.first.items.equipped => undefined method 'equipped' for #<ActiveRecord::Associations::CollectionProxy []>
User.first.items.armor.equipped => undefined method 'equipped' for #<ActiveRecord::AssociationRelation []>
如何委派给作用域?
【问题讨论】:
-
嗨 Matrix,我已经更新了我的答案,解释了为什么你实际上可能不想做你想做的事情,以及你需要什么方法来使用父范围检索子项目。
-
我一定是误解了这个问题,因为我从你的问题中确定
merge是解决方案。祝你好运!
标签: ruby-on-rails ruby-on-rails-4 scope delegates