1

我正在使用 rails 3.2.8 并且在多态关联方面遇到了一些麻烦。我有一个模型活动,它是多态的,它与另一个多态模型贡献相关,可以是资产文件、传记、动态注释或引用。

所以祖父是活动......父亲是贡献,最后(贡献的)儿子是传记,资产文件,动态注释或引文。

我的模型东西聚合了所有这些概念,我有下一个代码:

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

我得到了一个 void 结果,因为该对象不能有两种类型的 corse。

如果有人可以帮助我,我会很感激:)

提前致谢,

4

1 回答 1

0

我发现了一个 rails 问题:https ://github.com/rails/rails/issues/3882使用“alarribeau”解决问题的解决方法。这不是一个修复,但它可以帮助你。

于 2013-04-03T10:13:05.030 回答