2013-06-20 48 views
0

關係的has_many軌我有:與繼承

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 

我想成爲abble:

myCharacter.weapons

has_many :weapons, through: :character_items不工作,我只是想一樣的項目,但與過濾器「鍵入「欄只能獲得武器物件。

THX的幫助

PS:我on Rails的4

回答

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

是的,我知道條件的可能性,但他們不存在自動metod ?因爲如果我更改用於存儲名稱類(「類型」)的列的默認名稱值,我將不得不更改所有條件... – Matrix

+0

這是您在此處使用的哈希。關鍵只是一個象徵。你必須用測試來覆蓋它。另外,試着給列指定一個明確的名字,這樣你就不必在後面重命名了 – Tala

+0

ok,但是Rails 4就是這樣的:has_many:items,{through::character_items}, - >(weapons){where (type:'Weapon')} – Matrix