Rails多态关联- has_many条件错误



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

所以祖父是活动…父节点是贡献,子节点(贡献的子节点)是传记、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结果,因为对象不能有两种类型的对象

如果有人能帮我,我会很感激:)

提前致谢

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

相关内容

  • 没有找到相关文章

最新更新