message.last返回不同的结果

时间:2014-08-14 18:29:11

标签: ruby-on-rails

当我在实例列表上执行.last时,我看到了非常奇怪的结果。它似乎在改变。我要做的是检查收到的倒数第二条消息。这是控制台的输出非常奇怪:

irb(main):002:0> c.messages.last
=> #<Message id: 140426, user_id: nil, customer_id: 21672, text: "My personal coupon code doesn't work ", direction: "received", created_at: "2014-08-14 14:20:47", updated_at: "2014-08-14 14:20:47", viewed: true, company_id: 2, via_api: false, error_description: nil>
irb(main):003:0> c.messages[0]
=> #<Message id: 140426, user_id: nil, customer_id: 21672, text: "My personal coupon code doesn't work ", direction: "received", created_at: "2014-08-14 14:20:47", updated_at: "2014-08-14 14:20:47", viewed: true, company_id: 2, via_api: false, error_description: nil>
irb(main):004:0> c.messages[-1]
=> #<Message id: 130979, user_id: 4, customer_id: 21672, text: "You are officially a Mayvenn! To confirm your infor...", direction: "sent", created_at: "2014-08-11 18:02:18", updated_at: "2014-08-11 18:02:18", viewed: true, company_id: 2, via_api: true, error_description: nil>
irb(main):005:0> c.messages[1]
=> #<Message id: 140371, user_id: 4, customer_id: 21672, text: "Have upcoming appointments? Make sure to text them ...", direction: "sent", created_at: "2014-08-14 14:17:51", updated_at: "2014-08-14 14:17:51", viewed: true, company_id: 2, via_api: true, error_description: nil>
irb(main):006:0> c.messages.last
=> #<Message id: 130979, user_id: 4, customer_id: 21672, text: "You are officially signed up! To confirm your infor...", direction: "sent", created_at: "2014-08-11 18:02:18", updated_at: "2014-08-11 18:02:18", viewed: true, company_id: 2, via_api: true, error_description: nil>
&#34;您已正式注册!&#34;消息是该用户收到的第一条消息和我的个人优惠券代码...&#34;是最后一个。我在这做错了什么?

如何始终如一地获得倒数第二条消息?

1 个答案:

答案 0 :(得分:0)

与此question相似。您应该能够提取c.messages[-2]并检索倒数第二个(倒数第二个)消息,并且每次都应该相同(除非基础关联或对象发生更改)。