Ruby on Rails 数据库变量不起作用
我是 Rails 新手,我一直在基于朋友的代码工作: http://pastebin.com/6HwT6CBs
当我尝试操作一个变量(例如accuracy_ratio)时,Rails 会向我抛出错误。示例:
@post = Post.new(params[:post]) #These lines work fine
@post.user = current_user
@post.name = @post.user.name
@post.accuracy_ratio = 1.0 #This one throws an error
在 NetBeans Rails 上,当我键入“@post.”时,变量 user 和 name 会在自动完成帮助程序下方弹出,但不会弹出 precision_ratio。
我认为这是因为我需要 Rails 来重新配置我对表所做的更改。这是真的吗?如果没有,有谁知道我该如何解决这个问题?
编辑:非常感谢大家。正如我所说,我是红宝石磨砂膏。运行 rake db:migrate 后我就开始工作了。
I'm new to Rails, and I've been working based off of a friend's code: http://pastebin.com/6HwT6CBs
When I try to manipulate a variable (such as accuracy_ratio), Rails throws errors at me. Example:
@post = Post.new(params[:post]) #These lines work fine
@post.user = current_user
@post.name = @post.user.name
@post.accuracy_ratio = 1.0 #This one throws an error
On NetBeans Rails, when I type "@post.", the variables user and name pop up underneath the autocomplete helper, but not accuracy_ratio.
I think this is because I need Rails to reconfigure changes I make to the table. Is this true? If not, does anyone know how I can solve this?
edit: thanks so much guys. as i said, im a ruby scrub. after running rake db:migrate i got it working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Activerecord 将根据在数据库表中找到的内容更改您的模型。如果您已运行迁移并且表包含该列,则说明存在问题。您可以通过“重新加载!”来重新加载。并检查模型是否有新列。
您应该加载控制台并只需输入 Post 即可查看它有哪些字段。我对 Rails 的 netbeans 不太熟悉。我使用 Rubymine 和文本控制台。
Activerecord will change your model based on what it finds in the database table. If you have run the migration and the table contains the column, then there is something wrong. You can reload by "reload!" and check the model to see if it has the new column.
You should load up the console and just type Post to see what fields it has. I am not too familar with netbeans for Rails. I use Rubymine and the text console.
您确定“double”类型存在吗?看来您的迁移刚刚失败。使用浮点数或小数代替(取决于您的要求)。
Are you sure the type 'double' exists at all? Seems like your migration just failed. Use float or decimal instead(depending on your requirements).