我正在使用多态关联来跟踪我的项目中的注释.所有非常直接的东西.
我遇到的问题是基于多态关联查询并从Comment模型加入到它的所有者.
所以......
我有一个评论模型
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
和ForumTopics模式:
class ForumTopic < ActiveRecord::Base
has_many :comments, :as => :commentable
end
Run Code Online (Sandbox Code Playgroud)
我还有其他几个"可评论"的模型,现在并不重要.所有这一切都有效.
我想要做的是找到属于具有指定条件的ForumTopic的所有注释(在这种情况下,'featured'== true).
当我尝试使用finder加入模型时:
@comments = Comment.find(:all
:joins => :commentable
:conditions => ["forum_topics.featured = ? ", true]
)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
无法急切加载多态关联:可评论
使用AR"包含语法":
@comments = Comment.find(:all
:include => :forum_topics
:conditions => ["forum_topics.featured = ? ", true]
)
Run Code Online (Sandbox Code Playgroud)
收益:
未找到名为"forum_topics"的协会; 也许你拼错了吗?
如果我尝试使用表名而不是关联名(字符串而不是符号)加入:
@comments = Comment.find(:all,
:joins => "forum_topics",
:conditions => ["forum_topics.featured = ? ", true]
)
Run Code Online (Sandbox Code Playgroud)
我知道了:
Mysql …