0

所以我有一个Post模型和一个User模型。帖子属于用户,用户可以有很多帖子:

 => #<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 []>

user.posts我想我从来没有在 Rails 3 上看到过这个。在 Rails 4 中有什么不同的调用方式吗?

4

1 回答 1

1

如果您想要控制台内的实际结果,您应该添加to_a到最后:

user.posts.to_a
于 2013-07-07T18:11:28.190 回答