为模型编写范围、连接和顺序 [英] Writing scope, join and order for a Model

查看:30
本文介绍了为模型编写范围、连接和顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型是这样的:

计划 has_many 措施然后措施 has_many 目标并且 Target 表有一个名为 value

Program has_many Measures and then Measures has_many Targets and Target table has a column named value

我的查询是这样的:

@programs2 = Program.includes([measures: :targets])
               .some_scope
               .where('organization_id = 1')
               .limit(2)

我不知道在哪里或如何编写查询的 some_scope 部分.查询以 Program.includes 开头,所以我认为它应该在 Program 模型中定义,但我遇到的问题是 measures::targets .我如何为他们定义连接.如果只是一张桌子,我知道我可以这样做:

I don't know where or how to write the some_scope part of the query. The query starts with Program.includes so I think it should be defined in the Program model but the problem I have is that measures: :targets . How do I define a join for them. If it was just one table I know I can do like this:

scope :salary, :joins => :registry,   :order => "money DESC"

我也需要类似的东西,但这有两个表格,就像我上面解释的那样.我不知道怎么写那个.

I need something similar for this one too, but this has has two tables like I explained above. I don't know how to write that one.

推荐答案

尝试,

scope :some_scope, joins(mesures: :targets)

这篇关于为模型编写范围、连接和顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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