Rails的活动记录的查询双嵌套的联接与选择呼叫 [英] Rails Active Record Query for Double Nested Joins with a Select Call

查看:258
本文介绍了Rails的活动记录的查询双嵌套的联接与选择呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模式:

我使用的这个导轨服务器作为后端iOS应用程序,不断需要通知推送到组中的所有参与用户中的照片时添加。我的问题是找到最便宜的查询,以解决只的 User.ids 的影响。

I am using this rails server as a backend to an iOS application and constantly need to push out notifications to all involved users of a group when a photo is added. I problem is finding the least expensive query to resolve only the User.ids affected.

到目前为止,我有

    Photo.find(1).groups.joins(:users)

我知道,我必须在此之后把一个选择的说法,但不能找出语法我的生活。

I know that I have to put a select argument after this, but can't figure out the syntax for the life of me.

给定一个照片,我在寻找最有效的方式,找到受影响的用户ID的集合。

Given a photo, I am looking for the most efficient way to find a collection of the affected user id's.

任何帮助将是非常美联社preciated!

Any help would be much appreciated!!!

推荐答案

users_id = []
  Group.where(photo_id:1).users.collect {| U | users_id<< u.id}把users_id

users_id = []
Group.where(photo_id: 1).users.collect{|u| users_id << u.id} puts users_id

为照片的has_many组,以便belongs_to的照片组和gorups表已photo_id为外键

as Photo has_many groups so groups belongs_to photo and gorups table has photo_id as foreign key

所以请尝试这个

这篇关于Rails的活动记录的查询双嵌套的联接与选择呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆