所以我有一个发布模型和一个用户模型。帖子属于用户,用户可以有很多帖子:
=> #<Post id: 1, title: "First post", content: "first post", created_at: "2013-07-07 17:30:55", updated_at: "2013-07-07 17:30:55", user_id: 1>
1.9.3-p0 :015 > user
=> #<User id: 1, name: "Jano", email: "jano@gmail.com", created_at: "2013-07-07 17:31:41", updated_at: "2013-07-07 17:31:41">
post.user
到目前为止工作:
1.9.3-p0 :016 > post.user
=> #<User id: 1, name: "Jano", email: "jano@gmail.com", created_at: "2013-07-07 17:31:41", updated_at: "2013-07-07 17:31:41">
1.9.3-p0 :017 >
但是当我做user.posts
时:
1.9.3-p0 :017 > user.posts
Post Load (0.6ms) SELECT "posts".* FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy []>
1.9.3-p0 :018 >
我没有收到帖子,只有一个奇怪的#<ActiveRecord::Associations::CollectionProxy []>
。
我想我在Rails 3上从未见过这个。在Rails 4中调用user.posts
有什么不同的方法吗?
答案 0 :(得分:1)
如果您想在控制台中获得实际结果,则应在末尾添加to_a
:
user.posts.to_a