对于一个查询,我返回一个ActiveRecord:Realtion,使用另一个查询参数,我按预期和所需返回正确的数组。
如何在传递bot_client_id的值时获得数组,为什么行为会有所不同?
[20] pry(#<BotResponse>)> e = Event.where.has {(bot_client_id == 'aiaas-1409611358153-user-0147')}
=> #<Event::ActiveRecord_Relation:0x15fe2a8>
[21] pry(#<BotResponse>)> Event.where.has {(status == 'inactive')}.first
=> #<Event:0x00000002ae1d50
id: 1,
bot_client_id: "aiaas-1409611358153-user-0147",
keyword: "testing create event 1 minute from now",
topic: nil,
status: "inactive",
channel: "telegram",
created_date: 2017-05-06 20:37:24 UTC,
tickle_expression: nil,
time_of_day: "1 minute from now",
next_occurrence: 2017-05-06 20:54:20 UTC,
time_zone: nil,
recurring: false>发布于 2017-05-14 14:41:36
Active Record query interface几乎为每个方法调用都返回一个关系。这允许像Foo.where(bar: true).join(:baz).order(:id)一样链接方法调用。
另一个优点是,它不会立即对数据库运行查询,而是在第一次实际需要结果时运行。当您对关系调用某些方法(如each、map、count、to_a、load或Rails )时,Rails会加载记录
因此,您的示例之间的重要区别不是bot_id或status,而是在第二个示例中调用first,而不是在第一个示例中从数据库加载记录的方法。
https://stackoverflow.com/questions/43960753
复制相似问题