用活动记录连接多个表 [英] Join multiple tables with active records

查看:28
本文介绍了用活动记录连接多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道曾孙标签中的横幅名称.我的数据库是这样的:

I want to know the banner name situated in a great grandchildren tab. Here's how my data base is:

Inscription (where I need to start from): Item1_id
Item1: Item2_id
Item2: Banner_id
Banners: name

如何使用活动记录获取我的铭文表的所有横幅名称?

How do i get all the banners name of my inscription table with active records?

推荐答案

您可以执行以下操作:

Inscription.includes(item1: { item2: :banner })

关系名称item1item2banner 需要匹配给每个关系的名称.

The relations names item1, item2 and banner need to match the names given to each relation.

如果你想在这个查询上设置一个 where 语句,你可以这样做:

If you want to set a where statement on this query, you can do:

scope = Inscription.includes(item1: { item2: :banner }) 
scope = scope.where(banner: { name: "MOTD: Hello World!" })
scope = scope.where(item2: { is_favorite: true })

<小时>

类似问题:

这篇关于用活动记录连接多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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