Rails多态关联 - has_many条件错误

时间:2013-04-01 16:27:25

标签: ruby-on-rails activerecord polymorphic-associations has-many-polymorphs

我正在使用rails 3.2.8,而且我遇到了一些多态关联问题。 我有一个模型活动,它是多态的,与另一个多态模型贡献相关,可以是asset_file,传记,dynamic_annotation或引用。

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

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

  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。

如果有人可以帮助我,我会赞成:)

提前致谢,

1 个答案:

答案 0 :(得分:0)

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