加入与活跃记录的多个表 [英] Join multiple tables with active records

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

问题描述

我想知道坐落在一个曾孙标签的旗号。下面是我的数据库是:

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 项目2 横幅需要匹配给每个关系的名称

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

如果你想在这个查询设置了其中,语句,你可以这样做:

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 })


类似的问题:

  • <一个href="http://stackoverflow.com/questions/18082096/rails-4-scope-to-find-parents-with-no-children/18082147#18082147">Rails 4范围找父母没有子女
  • <一个href="http://stackoverflow.com/questions/23633301/how-to-query-a-model-based-on-attribute-of-another-model-which-belongs-to-the-fi/23633352#23633352">How查询一个模式的基础上,属于所述第一模型的另一模型的属性?中
  • <一个href="http://stackoverflow.com/questions/18234602/rails-active-record-querying-association-with-exists/18234998#18234998">Rails与活动记录查询协会'存在'
  • <一个href="http://stackoverflow.com/questions/13515225/rails-3-has-one-has-many-with-lambda-condition/13516285#13516285">Rails 3,HAS_ONE /的has_many与拉姆达条件
  • Rails 4 scope to find parents with no children
  • How to query a model based on attribute of another model which belongs to the first model?
  • Rails active record querying association with 'exists'
  • Rails 3, has_one / has_many with lambda condition

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

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