用活动记录连接多个表 [英] Join multiple tables with active records
本文介绍了用活动记录连接多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道曾孙标签中的横幅名称.我的数据库是这样的:
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 })
关系名称item1
、item2
和banner
需要匹配给每个关系的名称.
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 })
<小时>
类似问题:
- Rails 4 范围来查找父母没有孩子
- 如何根据属于第一个模型的另一个模型的属性查询模型?
- Rails 活动记录查询与存在"的关联一个>
- Rails 3, has_one/has_many with拉姆达条件
这篇关于用活动记录连接多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文