Rails 关系不适合我
我是 Rails 新手,正在学习如何使用它。
我遇到了这个问题,我不知道问题是什么。
我有 2 个类:
类 User < ActiveRecord::基础 has_one:博客 结束
课博客< ActiveRecord::基础 属于:用户 ActiveRecord
当我尝试执行 User.first.blogs 或 User.first.blog (不确定哪个是正确的)时,它告诉我
::StatementInvalid: SQLite3::SQLException: no such column: blogs.user_id: SELECT "blogs ".* FROM "blogs" WHERE ("blogs".user_id = 1)
我真的不知道为什么没有这样的专栏。据我所知,我认为活动记录应该为我处理这个问题,对吗?
I'm new to rails and am learning how to use it.
I came across this problem which i can't figure out what the problem is.
i have 2 classes:
class User < ActiveRecord::Base
has_one :blog
end
class Blog < ActiveRecord::Base
belongs_to :user
end
When i try to do User.first.blogs or User.first.blog (not sure which is correct), it tells me that
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: blogs.user_id: SELECT "blogs".* FROM "blogs" WHERE ("blogs".user_id = 1)
I really don't know why there's no such column like that. To my knowledge, i think active record is supposed to handle that for me, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在 blogs 表中添加外键列(user_id)。当您在rails中指定关联时,您需要将foreign_key列添加到关联表中。请查看此处有关 Rails 关联的完整文档
you have to add the foreign key column(user_id) in blogs table. when you specified the association in rails, you need to add foreign_key column into associated table. look here for complete document on rails associations