0
如果在阅读我的问题后,您有更好的标题建议,请添加注释。我无法简洁地说出我想要的东西。我有这样的情况。Activerecord Rails 4执行类似于连接的事情,仍然返回没有关联的行
class Artist < ActiveRecord::Base
has_many :album_artists
has_many :albums, :through => :album_artists
end
class Album < ActiveRecord::Base
has_many :album_artists
has_many :artists, :through => :album_artists
end
class AlbumArist < ActiveRecord::Base
belongs_to :album
belongs_to :artist
end
我要上artists
查询返回的结果如果任何艺术家的名字或专辑标题,艺术家与匹配查询相关。我可以通过连接来实现这一点。
Artist.joins(:albums).where("artists.name like ? or albums.title like ?", query, query).uniq
我想知道的是怎么也返回artists
其名称的查询相匹配,但不碰巧有与它们相关的任何albums
。我的目标是在单个查询中完成这一切,我不希望执行两个连续查询。
如果您需要,请索取更多说明。