Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 关系有许多与继承有关的轨道_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 关系有许多与继承有关的轨道

Ruby on rails 关系有许多与继承有关的轨道,ruby-on-rails,ruby,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 类字符

我有:

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
类字符
我想成为阿贝尔:

我的性格。武器

有很多:武器,通过::character\u物品
不起作用,我只想要与物品相同的物品,但使用“type”列过滤以仅获取武器对象

thx求救

附:我在轨道4上

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

是的,我知道条件的可能性,但它们不存在一个自动的方法?因为如果我更改用于存储名称类(“type”)的列的默认名称值,我将不得不更改所有条件……这是您在这里使用的哈希。钥匙只是一个符号。你必须用测试来覆盖它。另外,试着给你的列一个清晰的名称,这样你就不必在以后对它们进行重命名了,但是Rails 4是这样的:has_many:items,{through::character_items},->(武器){where(type:'wearm')}抱歉它:“has_many:wearms,{through::character_items,source::item},->{where(type:'wearm')}”好,我会把它添加到答案中,因为它可能也会帮助其他人