这是我在创建操作上运行PostsController规范时来自调试器的日志。我不会在运行时使用控制台更改或操作任何数据库记录,所以这不是特殊情况。
(rdb:1) p Post.last
#<Post id: 2, author_id: 3, title: "My ultra cool post", content: "Lorem ipsum", deleted_at: nil, created_at: "2012-08-31 09:21:20", updated_at: "2012-08-31 09:21:20", published_at: nil, display_from: nil>
(rdb:1) p Post.all
[#<Post id: 2, author_id: 3, title: "My ultra cool post", content: "Lorem ipsum", deleted_at: nil, created_at: "2012-08-31 09:21:20", updated_at: "2012-08-31 09:21:20", published_at: nil, display_from: nil>,
#<Post id: 3, author_id: 2, title: "My ultra cool post", content: "Lorem ipsum", deleted_at: nil, created_at: "2012-08-31 09:21:20", updated_at: "2012-08-31 09:21:20", published_at: nil, display_from: nil>]
(rdb:1) p Post.last
#<Post id: 2, author_id: 3, title: "My ultra cool post", content: "Lorem ipsum", deleted_at: nil, created_at: "2012-08-31 09:21:20", updated_at: "2012-08-31 09:21:20", published_at: nil, display_from: nil>
所以 Post.all 显示帖子 3 存在,但 Post.last 不存在
但是当我这样做时
(rdb:1) p Post.all.last
#<Post id: 3, author_id: 2, title: "My ultra cool post", content: "Lorem ipsum", deleted_at: nil, created_at: "2012-08-31 09:21:20", updated_at: "2012-08-31 09:21:20", published_at: nil, display_from: nil>
控制器文件:
class PostsController < ApplicationController
def create
@post.author = current_user
@post.save
debugger # this is where I'm debugging
respond_with @post
end
end
- 此行为在开发代码中正确工作
对我来说唯一的含义是在我的规范中我会做
response.should redirect_to Post.all.last
安装的
response.should redirect_to Post.last
我知道在 Ralis 中有一些情况下 Active Record 触发器在 DB 准备好之前调用?但在这种情况下,ActiveRecord 实际上通过 .all 调用而不是 .last 知道该记录的存在,所以这很奇怪
对此有什么想法吗?