所以我有一个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 中有什么不同的调用方式吗?