控制台中的关联关系

时间:2016-06-21 18:03:49

标签: ruby-on-rails associations

好的,尝试在控制台

中学习关联关系的rails命令

因此,如果我有has_many帖子的用户,以及belongs_to用户...

的帖子

这意味着所有这一切都应该有效,对吗?这些 all 为这种特殊关系自动添加的方法,对吗?

1. user.posts
2. user.posts=(posts)
3. user.posts << post
4. user.posts.delete(post)
5. user.posts.empty?
6. user.posts.size
7. user.post_ids
8. user.posts.clear
9. user.posts.find
10. user.posts.build(attributes={})
11. user.posts.create(attributes={})

user是:user = User.create(:name => "Michael")

结果是:#<User id: 3, name: "Michael", created_at: "2016-06-21 16:52:22", updated_at: "2016-06-21 16:52:22">

&安培;

post是:post = Post.create(:body => "body of txt")

结果是:#<Post id: 3, user_id: 3, body: "body of txt", created_at: "2016-06-21 16:53:51", updated_at: "2016-06-21 16:58:38">

1。对于user.posts,我得到了这个:

#<ActiveRecord::Associations::CollectionProxy [#<Post id: 3, user_id: 3, body: "body of txt", created_at: "2016-06-21 16:53:51", updated_at: "2016-06-21 16:58:38">, #<Post id: nil, user_id: 3, body: "txt of body2", created_at: nil, updated_at: nil>]>

2。对于user.posts=(posts),我得到了这个:

NameError: undefined local variable or method posts' for main:Object and a whole bunch more of rbenv stuff

第3。对于user.posts << post,我得到了这个:

#<ActiveRecord::Associations::CollectionProxy [#<Post id: 3, user_id: 3, body: "body of txt", created_at: "2016-06-21 16:53:51", updated_at: "2016-06-21 16:58:38">, #<Post id: nil, user_id: 3, body: "txt of body2", created_at: nil, updated_at: nil>, #<Post id: 3, user_id: 3, body: "body of txt", created_at: "2016-06-21 16:53:51", updated_at: "2016-06-21 16:58:38">]>

5。对于user.posts.empty?,我得到了这个:

false

6。对于user.posts.size,我得到了这个:

3

7。对于user.post_ids,我得到了这个:

[3, nil, 3]

9。对于user.posts.find,我得到了这个:

ActiveRecord::RecordNotFound: Couldn't find Post without an ID

10。对于user.posts.build(attributes={:body => "random body of txt"}),我得到了这个:

#<Post id: nil, user_id: 3, body: "random body of txt", created_at: nil, updated_at: nil>

关于上述问题的有趣之处在于,当我Post.all时,它并没有显示出来!但是,如果我user.posts,它确实出现在...... {/ p>

11。对于user.posts.create(attributes={}),我得到了这个:

=> #<Post id: 4, user_id: 3, body: "body of text via create method", created_at: "2016-06-21 17:49:50", updated_at: "2016-06-21 17:49:50">

4。对于user.posts.delete(post),我得到了这个:

(0.3ms) begin transaction SQL (0.6ms) DELETE FROM "posts" WHERE "posts"."id" = ? [["id", 3]] (1.8ms) commit transaction => [#<Post id: 3, user_id: 3, body: "body of txt", created_at: "2016-06-21 16:53:51", updated_at: "2016-06-21 16:58:38">]

8。对于user.posts.clear,我得到了这个:

DELETE FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 3]]

这意味着现在,当我运行这个:user.posts时,我明白了:

#<ActiveRecord::Associations::CollectionProxy []>

所以,正如你从上面所看到的那样,#2 不起作用......以及#9 。为什么不?

提前感谢您的帮助!

编辑回答答案(更容易澄清)

好的,我做了posts = user.posts,结果是:

#<ActiveRecord::Associations::CollectionProxy [#<Post id: 5, user_id: 3, body: "body of txt", created_at: "2016-06-21 18:00:57", updated_at: "2016-06-21 18:01:11">, #<Post id: 6, user_id: 3, body: "body of txt2", created_at: "2016-06-21 18:22:02", updated_at: "2016-06-21 18:22:36">]>

然后我做了user.posts=(posts),得到了与上面相同的答案。

但是当我跑posts = Post.where(user: user)时,我得到了这个:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: posts.user: SELECT "posts".* FROM "posts" WHERE "posts"."user" = 3

另外,当我跑user.posts = posts时,我得到了这个:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: posts.user: SELECT "posts".* FROM "posts" WHERE "posts"."user" = 3

关于user.posts.find(Post.last.id),如果我这样做了,那就有用了:

user.posts.find(Post.last)但如果我做了这样的事情:

user.posts.find(Post.last.6),我明白了:

SyntaxError: (irb):121: no .<digit> floating literal anymore; put 0 before dotuser.posts.find(Post.last.6)

1 个答案:

答案 0 :(得分:0)

#2

你需要定义什么&#34;帖子&#34;是

如果你想&#34;设置&#34;讯息

posts = user.posts

如果你想对两者做一个比较,你可以这样做:

posts = Post.where(user: user)
user.posts == posts #should be true

#9

您需要将一些内容传递给find方法:

user.posts.find(Post.last) #this returns an object if "user" created Post.last

希望这有帮助!

编辑:

你不能做user.posts =(posts),因为你用数组,哈希,字符串,整数等设置数据。

等于比较运算符应该有2 =&#39; s。这是我的错误,我纠正了这个帖子。