我在围绕多个关联使用.joins时遇到了问题。这是我的模型设置:Rails多个连接返回空集。为什么?
class Article
has_many :comments
has_many :tags
end
class Comment
belongs_to :article
end
class Tag
belongs_to :article
end
我试图找到具有特定注释的身体或与特定标签的文本标签注释的所有文章。这是我的查询:
Article.joins(:tags, :comments).where("(\"tags\".\"tag\" = 'awesome') OR (\"comments\".\"body\" = 'hello')
它总是返回一个空的结果。最奇怪的是,这将工作:
Article.joins(:tags).where(:tags => {:tag => "awesome:})
但当我添加其他连接符号,什么都不会返回:
Article.joins([:tags, :comments]).where(:tags => {:tag => "awesome"})
这是否任何意义?我在做一些愚蠢的事情吗?
是否这样的情况下,一些标签文章可能没有评论(或相反,一些评论文章可能没有标签)? – 2013-02-11 16:56:49