0

我有 :

class Character < ActiveRecord::Base
  has_many :items, through: :character_items
  has_many :character_item
end

class Item < ActiveRecord::Base
class Weapon < Item
class Armor < Item

我想成为能人:

myCharacter.weapons

has_many :weapons, through: :character_items不工作,我只想要与项目相同但使用“类型”列过滤以仅获取武器对象。

谢谢帮助

PS:我在 Rails 4

4

1 回答 1

1
has_many :weapons, through: :character_items, conditions: {character_items: {type: "weapon"}}, class_name: "Item", source: :item

希望有帮助

编辑 来自矩阵的答案:

 has_many :weapons, { through: :character_items, source: :item }, -> { where(type: 'Weapon') } 
于 2013-06-20T16:06:58.487 回答