简体   繁体   English

如何连接多个桌子导轨

[英]How to join more than one table rails

I've got a bit of code that I'm looking at the moment : 目前,我正在查看一些代码:

User.includes([:profile => :schedule]).where('...')

This loads all profiles and their schedules. 这将加载所有配置文件及其时间表。 So the query produced by this is select * user fields, select * profile fields, select * schedule fields 因此,由此产生的查询是选择*用户字段,选择*个人资料字段,选择*计划字段

I'm filtering out users based on one field from profile and based on one field from profiles schedule but this above statement is selecting all the fields from all of these. 我根据配置文件中的一个字段和配置文件计划中的一个字段过滤掉用户,但是以上声明是从所有这些字段中选择所有字段。

So I tried to do it with joins : 所以我试图用joins做到这一点:

User.joins(:profile).joins('profiles.schedule').where('....').select('....')

This is throwing out error. 这是抛出错误。 I'm a former Java developer, still learning the rails stuff. 我以前是Java开发人员,仍然在学习Rails的东西。

这应该工作:

User.joins(profile: :schedule).where('....').select('....')

Try this: 尝试这个:

User.joins(profile: [:schedule]).where('...')

or 要么

User.joins(profile: :schedule).where('...')

如果您已将您的Profile模型设置为通过has_many通过关联来进行关联Schedule ,则可以使用以下代码:

User.joins(:schedule).where('...')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM