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
不起作用,我只想和项目一样,但过滤“type”列只能获得武器对象。
请求帮助
PS:我在Rails 4上
答案 0 :(得分:1)
has_many :weapons, through: :character_items, conditions: {character_items: {type: "weapon"}}, class_name: "Item", source: :item
希望有所帮助
修改强> Matrix回答:
has_many :weapons, { through: :character_items, source: :item }, -> { where(type: 'Weapon') }