关系has_many rails与继承

时间:2013-06-20 15:51:34

标签: ruby-on-rails ruby

我有:

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上

1 个答案:

答案 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') }