简体   繁体   English

Ruby on Rails通过api查询从模型中获取属性

[英]Ruby on Rails get an attribute from model via api query

The answer to this question should be simple, but I haven't found one through the active record querying guide , other questions here on SO, or through messing around in the Rails console. 这个问题的答案应该很简单,但是我没有通过活动记录查询指南 ,SO上的其他问题,或者通过在Rails控制台中搞乱来找到答案。

I simply want to query the database through the active record querying interface and return the value of a single column of the first or last entry, without having to traverse through the entire table (will explain in a moment). 我只想通过活动记录查询接口查询数据库并返回第一个或最后一个条目的单个列的值,而不必遍历整个表(稍后会解释)。

There is a way to do this with pluck, however the structure of the query messages are as follows: 有一种方法可以使用pluck执行此操作,但查询消息的结构如下:

    initial = Message.where("id = ?", some_id).pluck(:value).first
    final   = Message.where("id = ?", some_id).pluck(:value).last

Unfortunately, this is an extremely inefficient operation as it plucks the value attribute out of every record where there is a match on the provided id , before returning either just the first or last entry. 不幸的是,这是一个非常低效的操作,因为它在返回所提供的id匹配的每个记录之外从所有记录中提取value属性,然后返回第一个或最后一个条目。 I would like to basically reorder the statements to be something along the lines of: 我想基本上将语句重新排序为:

    initial = Message.where("id = ?", some_id).first.pluck(:value)
    final   = Message.where("id = ?", some_id).last.pluck(:value)

However, I get an NoMethodError explaining there is no method pluck for Message. 不过,我得到一个NoMethodError解释没有方法pluck的消息。 I've tried to do this various ways: 我试过各种各样的方式:

    initial = Message.where("id = ?", some_id).first(:value)
    initial = Message.where("id = ?", some_id).first.select(:value)
    ...

But all return some sort of error. 但都会返回某种错误。 I know returning the 我知道回来了

Oops 哎呀
Somehow part of my question got cut off (including the answer I had at the end) - I'll see if I can find it, but I explored using the select() method, in which I found that select only takes one argument meaning a query string must be built as it cannot take optional arguments like id = ?, some_id , but then I found that just appending a .value (where value is the column attribute that you are trying to get) works, so I switched back to the where method as shown in the answer below. 不知怎的,我的问题的一部分被切断了(包括我最后的答案) - 我会看看我是否能找到它,但我探索了使用select()方法,其中我发现select只接受一个参数含义必须构建一个查询字符串,因为它不能使用像id = ?, some_id这样的可选参数,但后来我发现只是追加一个.value (其中value是你想要获取的列属性),所以我切换回where方法如下面的答案所示。

Answer is in the question, but if you're trying to do something like this: 答案就在问题中,但是如果你想做这样的事情:

    initial = Message.where("id = ?", some_id).pluck(:value).first
    final   = Message.where("id = ?", some_id).pluck(:value).last

Change it to this (just reference the column name, in this example it is value , but it could be amount or something): 将其更改为此(仅引用列名称,在此示例中它是value ,但它可能是amount或其他):

    initial = Message.where("id = ?", some_id).first.value
    final   = Message.where("id = ?", some_id).last.value

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

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