【问题标题】:Slight query issue, when trying to do a join table with rails 2.3.5尝试使用 rails 2.3.5 连接表时出现轻微查询问题
【发布时间】:2012-12-17 20:03:58
【问题描述】:

我把这个查询放在一起,效果很好。

     search = Artist.where(:status => "active").where("upcoming_events_count > 0").order("echonest_external_popularity DESC").limit(limit))

问题是我想再添加一张支票,该支票位于另一张 ArtistPhoto 表上。匹配需要确保艺术家(来自artist_id)也包含一张有效的照片。

我有独立工作的查询:

     ArtistPhoto.where("artist_id = ? and artist_photos.primary = ?", self.id, true).first

如果有人可以帮助将连接加入 1 个查询,那就太好了。

谢谢

【问题讨论】:

    标签: mysql ruby-on-rails join


    【解决方案1】:

    使用joins添加其他关联:

    search = Artist.joins(:artist_photos).
                    where("artist_photos.primary = ?", true).
                    where(...) # other filters here
    

    此答案假设您的 Artist 模型上有一个 has_many :artist_photos

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 2013-03-27
      • 2022-11-26
      相关资源
      最近更新 更多