简体   繁体   English

如何查询ActiveModel记录的子集?

[英]How do I query on a subset of ActiveModel records?

I've rewritten this question as my previous explanation was causing confusion. 我已经重写了这个问题,因为我先前的解释引起了混乱。

In the SQL world, you have an initial record set that you apply a query to. 在SQL世界中,您具有要对其应用查询的初始记录集 The output of this query is the result set . 该查询的输出是结果集 Generally, the initial record set is an entire table of records and the result set is the records from the initial record set that match the query ruleset. 通常, 初始记录集是整个记录表, 结果集是来自初始记录集的与查询规则集匹配的记录

I have a use case where I need my application to occasionally operate on only a subset of records in a table. 我有一个用例,需要我的应用程序偶尔仅对表中的记录子集进行操作。 If a table has 10,000 records in it, I'd like my application to behave like only the first 1,000 records exist. 如果表中有10,000条记录,我希望我的应用程序的行为就像仅存在前1,000条记录一样。 These should be the same 1,000 records each time. 每次应具有相同的1,000条记录。 In other words, I want the initial record set to be the first 1,000 devices in a table (when ordered by primary key), and the result set the resulting records from these first 1,000 devices. 换句话说,我希望初始记录集是表中的前1,000个设备(按主键排序时),而结果集则来自这前1,000个设备的结果记录。

Some solutions have been proposed, and it's revealed that my initial description was not very clear. 已经提出了一些解决方案,并且发现我的最初描述不是很清楚。 To be more explicit, I am not trying to implement pagination. 更明确地说,我不是要实现分页。 I'm also not trying to limit the number of results I receive (which .limit(1,000) would indeed achieve). 我也没有试图限制我收到的结果的数量( .limit(1,000)的确可以实现)。

Thanks! 谢谢!

This is the line in your question that I don't understand: 这是您不明白的问题:

This causes issues though with both of the calls, as limit limits the results of the query, not the database rows that the query is performed on. 尽管这两个调用都引起问题,因为限制限制了查询的结果,而不是查询所依据的数据库行。

This is not a Rails thing, this is a SQL thing. 这不是Rails的事情,这是SQL的事情。

Device.limit(n) runs SELECT * FROM device LIMIT n Device.limit(n)运行SELECT * FROM device LIMIT n

Limit always returns a subset of the queried result set. 限制始终返回查询结果集的子集。

Would first(n) accomplish what you want? first(n)完成你想要的吗? It will both order the result set ascending by the PK and limit the number of results returned. 它将对结果集按PK升序排序,并限制返回的结果数。

SQL Statements can be chained together. SQL语句可以链接在一起。 So if you have your subset, you can then perform additional queries with it. 因此,如果有子集,则可以对其执行附加查询。

my_subset = Device.where(family: "Phone")
# SQL: SELECT * FROM Device WHERE `family` = "Phone"

my_results = my_subset.where(style: "Touchscreen")
# SQL: SELECT * FROM Device WHERE `family` = "Phone" AND `style` = "Touchscreen"

Which can also be written as: 也可以写成:

my_results = Device.where(family: "Phone").where(style: "Touchscreen")
my_results = Device.where(family: "Phone", style: "Touchscreen")
# SQL: SELECT * FROM Device WHERE `family` = "Phone" AND `style` = "Touchscreen"

From your question, if you'd like to select the first 1,000 rows (ordered by primary key, pkey ) and then query against that, you'll need to do: 从您的问题开始,如果您想选择前1,000行(按主键pkey排序),然后对其进行查询,则需要执行以下操作:

my_results = Device.find_by_sql("SELECT * 
                           FROM (SELECT * FROM devices ORDER BY pkey ASC LIMIT 1000) 
                           WHERE `more_searching` = 'happens here'")

You could specifically ask for a set of IDs: 您可以专门要求一组ID:

Device.where(id: (1..4).to_a)

That will construct a WHERE clause like: 这将构造一个WHERE子句,例如:

WHERE id IN (1,2,3,4)

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

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