我正在使用rails,我有两个表Post和Tags with habtm relation。
以下句子有效:
@posts = Post.find(:all, :include=>:tags, :conditions => ['tags.term LIKE ? OR tags.term LIKE ?', "%Barcelona%", "%restaurante%" ])
或
@posts = Post.find(:all, :include=>:tags, :conditions => ['tags.term in (?)', ['Barcelona','restaurante'] ])
但是这个AND
条件不起作用:
@posts = Post.find(:all, :include=>:tags, :conditions => ['tags.term LIKE ? AND tags.term LIKE ?', "%Barcelona%", "%restaurante%" ])
我想知道获得包含tags.term
的所有帖子的句子:“Barcelona and restaurante”
提前致谢
埃米利奥
答案 0 :(得分:0)
我回应自己,为我工作,希望可以帮助某人:
@posts = Post.find_by_sql(“select * from posts p where(select count(distinct t.id)from posts_tags pt left join tags t on pt.tag_id = t.id where t.term in('Barcelona' ,'restaurante')和pt.post_id = p.id)> = 2“)