1

我使用的鐵軌3.2.8,我有一些麻煩與多態關聯。 我有一個模型活動,它是多態的,與另一個可以是asset_file,biography,dynamic_annotation或引用的多態模型貢獻相關。Rails的多態關聯 - 的has_many條件錯誤

所以爺爺是活動......父親是貢獻(貢獻)最後兒子是傳記,asset_file,dynamic_annotations或引用。

我是聚集所有的這些概念我有下面的代碼我的模型的事:我打開控制檯,我輸入下一個查詢 :

has_many :activities, 
      :order => 'created_at DESC', 
      :dependent =>:delete_all 

    has_many :contributions, 
      :through => :activities, 
      :source => :activable, 
      :source_type => "Contribution" 

    has_many :dynamic_annotations, 
      :through => :contributions, 
      :source => :contributable, 
      :source_type => "DynamicAnnotation", 
      :conditions => {"contributions.c_state" => "ACCEPTED"} 

    has_many :biographies, 
      :through => :contributions, 
      :source => :contributable, 
      :source_type => "Biography", 
      :conditions => {"contributions.c_state" => "ACCEPTED"} 

    has_many :citations, 
      :through => :contributions, 
      :source => :contributable, 
      :source_type => "Citation", 
      :conditions => {"contributions.c_state" => "ACCEPTED"} 

    has_many :asset_files, 
      :through => :contributions, 
      :source => :contributable, 
      :source_type => "AssetFile", 
      :conditions => {"contributions.c_state" => "ACCEPTED"} 

問題

Thing.first.biographies 

的我得到的SQL:

SELECT `biographies`.* FROM `biographies` INNER JOIN `contributions` ON `biographies`.`id` = `contributions`.`contributable_id` INNER JOIN `activities` ON `contributions`.`id` = `activities`.`activable_id` WHERE `activities`.`thing_id` = 1 AND `activities`.`activable_type` = 'Contribution' AND (`contributions`.`c_state` = 'ACCEPTED') AND (`contributions`.`contributable_type` = 'Biography') 

這是一個正確的答案

然後我的命令後鍵入:

Thing.first.dynamic_annotations 

,我得到的SQL:

SELECT `dynamic_annotations`.* FROM `dynamic_annotations` INNER JOIN `contributions` ON `dynamic_annotations`.`id` = `contributions`.`contributable_id` INNER JOIN `activities` ON `contributions`.`id` = `activities`.`activable_id` WHERE `activities`.`thing_id` = 1 AND `activities`.`activable_type` = 'Contribution' AND (`contributions`.`c_state` = 'ACCEPTED') AND (`contributions`.`contributable_type` = 'Biography' AND `contributions`.`contributable_type` = 'DynamicAnnotation') 

我得到了一個空白的結果,因爲對象不能有兩種類型的科西嘉的。

如果有人可以幫助我,我將apreciate提前:)

感謝,

回答