Ruby ActiveRecord查询取回ActiveRecord:关系
对于一个查询,我得到了一个ActiveRecord:Realtion,通过另一个查询参数,我得到了预期和期望的正确数组 传入bot_client_id的值时,如何获取数组,为什么行为会有所不同Ruby ActiveRecord查询取回ActiveRecord:关系,ruby,activerecord,Ruby,Activerecord,对于一个查询,我得到了一个ActiveRecord:Realtion,通过另一个查询参数,我得到了预期和期望的正确数组 传入bot_client_id的值时,如何获取数组,为什么行为会有所不同 [20] pry(#<BotResponse>)> e = Event.where.has {(bot_client_id == 'aiaas-1409611358153-user-0147')} => #<Event::ActiveRecord_Relation:0x15f
[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>
[20]pry(#)>e=Event.where.has{(bot_client_id='aiaas-1409611358153-user-0147'))
=> #
[21]pry(#)>Event.where.has{(status=='inactive')}。首先
=> #
返回几乎每个方法调用的关系。这允许链接方法调用,如Foo.where(bar:true).join(:baz).order(:id)
另一个优点是它不会立即对数据库运行查询,而是在第一次实际需要结果时运行查询。Rails在对关系调用某些方法时加载记录,例如每个
,映射
,计数
,到a
,加载
或首先
因此,您的示例之间的重要区别不是bot\u id
或status
,而是您在第二个示例中调用了first
,但在第一个示例中没有方法会从数据库加载记录。几乎每个方法调用都会返回一个关系。这允许链接方法调用,如Foo.where(bar:true).join(:baz).order(:id)
另一个优点是它不会立即对数据库运行查询,而是在第一次实际需要结果时运行查询。Rails在对关系调用某些方法时加载记录,例如每个
,映射
,计数
,到a
,加载
或首先
因此,示例之间的重要区别不是bot\u id
或status
,而是在第二个示例中调用first
,但在第一个示例中没有从数据库加载记录的方法