has_many_polymorphs 问题

发布于 2024-09-01 03:55:51 字数 3234 浏览 2 评论 0原文

我对 has_many_polymorphs 有疑问,也许你可以帮助我。

我有一些模型

class Issue < ActiveRecord::Base
  has_many_polymorphs :things, :from => [:applications], :through => :relations
ene

class NewsArticle < ActiveRecord::Base
  has_many_polymorphs :things, :from => [:applications], :through => :relations
end

class Application < ActiveRecord::Base
  has_many_polymorphs :contents, :from => [:news_articles, :issues], :through => :relations
end  

class Relation < ActiveRecord::Base
  belongs_to :content, :polymorphic => true
  belongs_to :thing, :polymorphic => true

  acts_as_double_polymorphic_join(
   :contents => [:news_articles, :issues],
   :things => [:applications]
  )
end

,但在尝试获取某些问题的应用程序时,我

>> i = Issue.first
+----+--------+---------+----------+---------------+----------------+-------------+-------------+-----------------+---------------------------+---------------------------+
| id | closed | user_id | answered | answers_count | comments_count | views_count | flags_count | favorites_count | created_at                | updated_at                |
+----+--------+---------+----------+---------------+----------------+-------------+-------------+-----------------+---------------------------+---------------------------+
| 1  | false  | 2       |          | 2             | 2              | 0           | 0           | 0               | 2010-04-22 00:14:56 +0400 | 2010-05-01 02:35:30 +0400 |
+----+--------+---------+----------+---------------+----------------+-------------+-------------+-----------------+---------------------------+---------------------------+
1 row in set
>> i.applications
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :application in model Relation.  Try 'has_many :applications, :through => :relation, :source => <name>'.  Is it one of :content or :thing?
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:289:in `check_validity!'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/has_many_through_association.rb:5:in `initialize'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `new'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `applications'
        from (irb):2
        from :0
>> 

在日志中看到以下内容,我看到

** has_many_polymorphs: autoload hook invoked
** has_many_polymorphs: preloading parent model Application
** has_many_polymorphs: preloading parent model Issue
** has_many_polymorphs: preloading parent model NewsArticle
** has_many_polymorphs: preloading parent model Relation
** has_many_polymorphs: autoload hook invoked
** has_many_polymorphs: preloading parent model Application
** has_many_polymorphs: preloading parent model Issue
** has_many_polymorphs: preloading parent model NewsArticle
** has_many_polymorphs: preloading parent model Relation
  Issue Load (0.9ms)   SELECT * FROM "issues" LIMIT 1

如何解决它?

PS:Rails 2.3.5、Postgresql

i have problem with has_many_polymorphs, maybe you can help me.

I have some models

class Issue < ActiveRecord::Base
  has_many_polymorphs :things, :from => [:applications], :through => :relations
ene

class NewsArticle < ActiveRecord::Base
  has_many_polymorphs :things, :from => [:applications], :through => :relations
end

class Application < ActiveRecord::Base
  has_many_polymorphs :contents, :from => [:news_articles, :issues], :through => :relations
end  

class Relation < ActiveRecord::Base
  belongs_to :content, :polymorphic => true
  belongs_to :thing, :polymorphic => true

  acts_as_double_polymorphic_join(
   :contents => [:news_articles, :issues],
   :things => [:applications]
  )
end

but on attempt to get applications for some issue i see following

>> i = Issue.first
+----+--------+---------+----------+---------------+----------------+-------------+-------------+-----------------+---------------------------+---------------------------+
| id | closed | user_id | answered | answers_count | comments_count | views_count | flags_count | favorites_count | created_at                | updated_at                |
+----+--------+---------+----------+---------------+----------------+-------------+-------------+-----------------+---------------------------+---------------------------+
| 1  | false  | 2       |          | 2             | 2              | 0           | 0           | 0               | 2010-04-22 00:14:56 +0400 | 2010-05-01 02:35:30 +0400 |
+----+--------+---------+----------+---------------+----------------+-------------+-------------+-----------------+---------------------------+---------------------------+
1 row in set
>> i.applications
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :application in model Relation.  Try 'has_many :applications, :through => :relation, :source => <name>'.  Is it one of :content or :thing?
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:289:in `check_validity!'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/has_many_through_association.rb:5:in `initialize'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `new'
        from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `applications'
        from (irb):2
        from :0
>> 

in logs i see

** has_many_polymorphs: autoload hook invoked
** has_many_polymorphs: preloading parent model Application
** has_many_polymorphs: preloading parent model Issue
** has_many_polymorphs: preloading parent model NewsArticle
** has_many_polymorphs: preloading parent model Relation
** has_many_polymorphs: autoload hook invoked
** has_many_polymorphs: preloading parent model Application
** has_many_polymorphs: preloading parent model Issue
** has_many_polymorphs: preloading parent model NewsArticle
** has_many_polymorphs: preloading parent model Relation
  Issue Load (0.9ms)   SELECT * FROM "issues" LIMIT 1

How to solve it?

PS: Rails 2.3.5, Postgresql

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-09-08 03:55:51

我已经完成了这个问题:)问题是 - 文档不包含信息,连接的模型(在本期中 - Issue、NewsArticle、Application)不应包含任何有关关系的内容(例如 has_many_polymorphs :things, :from => [:应用程序],:通过=>)

I've done this issue :) The problem is - that documentation does not contains information, that connected models (in this issue - Issue, NewsArticle, Application) should not contain anything about relations (like has_many_polymorphs :things, :from => [:applications], :through => :relation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文