2010-05-05 47 views
1

我有has_many_polymorphs問題,也許你可以幫助我。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

+0

不應該是'i.things'嗎? – 2010-05-05 20:43:17

+0

>> i.things Hirb錯誤:PGError:錯誤:column relations.issue_id不存在 LINE 1:... FROM關係JOIN發出polymorphic_parent ON關係.... – 2010-05-06 06:20:42

回答

2

我做了這個問題:)問題是 - 這文件不包含信息,即連接模式(在這個問題上 - 問題,NewsArticle,應用)應不包含任何關係(如has_many_polymorphs:things,:from => [:applications],:through =>:relation)