Ruby on Rails,活动记录更新

发布于 2024-11-16 19:59:37 字数 767 浏览 9 评论 0原文

正在编写 Ruby on Rails 教程。我刚刚注意到,在生成迁移时,我拼错了其中一个列名称。当我尝试与关联合作时,我收到此错误。

 first_page = Page.new(:name => "first page", :permalink => 'first', :position => 1)
ActiveRecord::UnknownAttributeError: unknown attribute: position
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1564:in `attributes='
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1560:in `each'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1560:in `attributes='
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1412:in `initialize'
    from (irb):5:in `new'
    from (irb):5
>> 

我的问题是如何使用职位的正确拼写更新活动记录。我已经在我的架构文件中更改了它。

working on a tutorial for Ruby on Rails. I just noticed that I had miss-spelled one of my column names when I generated my migrations. When I try to work with associations I get this error.

 first_page = Page.new(:name => "first page", :permalink => 'first', :position => 1)
ActiveRecord::UnknownAttributeError: unknown attribute: position
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1564:in `attributes='
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1560:in `each'
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1560:in `attributes='
    from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.8/lib/active_record/base.rb:1412:in `initialize'
    from (irb):5:in `new'
    from (irb):5
>> 

My question is how do I update active record with the correct spelling of the Position. I've changed it in my Schema file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

微凉 2024-11-23 19:59:37

您应该使用迁移功能来更新架构,而不是手动执行此操作。

在此处查找更多信息:http://guides.rubyonrails.org/migrations.html

You should use the migration feature to update your schema rather than doing that manually.

Find more info here: http://guides.rubyonrails.org/migrations.html

玩物 2024-11-23 19:59:37

如果您已经提交/推送了迁移,那么其他人可能已经执行了它,因此您最好使用新的迁移来解决问题,来自@Ed Haywood:

rename_column :table, :old_column_name, :new_column_name

如果您没有共享此迁移,则可以修复迁移而不是创建一个新的。撤消上次迁移

rake db:rollback

然后修复迁移并再次运行 rake db:migrate

您应该永远不要手动编辑 schema.rb,它会在每次运行迁移时更新。在您没有编辑它的情况下,当您解决问题时它应该是正确的。

If you already commit/push your migration, it is possible than other people already execute it so you are better to fix the problem with a new migration, from @Ed Haywood :

rename_column :table, :old_column_name, :new_column_name

If you do not have shared this migration, you can fix the migration instead of creating a new one. To undo the last migration

rake db:rollback

Then fix your migration and run it again rake db:migrate

You should never edit your schema.rb manually, it is updated each time you run a migration. In your case without editing it, it should be correct when you have fixed the problem.

英雄似剑 2024-11-23 19:59:37

切勿手动编辑架构。这不会对您的数据库表进行任何更改。

只需运行新的迁移来重命名列:

rename_column :table, :old_column_name, :new_column_name

要回答您的问题“如何更新 Active Record”,您对您的术语有点不清楚。您的意思是“如何更新数据库表”?如果是这样,答案是回滚迁移并修复它,或者运行新的迁移。两者都可以,但回滚可能会导致生产模式下的数据丢失(我认为这不是您的情况)。

要更改模型,您只需在文本编辑器中打开 ruby​​ 文件并进行更改即可。

Never edit the schema manually. That will not make any changes to your database tables.

Simply run a new migration to rename the column:

rename_column :table, :old_column_name, :new_column_name

To answer your question "How do I update Active Record", you're being a bit unclear with your terms. Do you mean "How do I update the database table"? If so, the answer is either rollback your migration and fix it, or run a new migration. Either will work, but rollbacks can cause data loss in production mode (which I assume is not your case).

To change the models you simply open the ruby file in a text editor and change it.

一生独一 2024-11-23 19:59:37

修复您的初始迁移以获得正确的拼写,然后运行:

$ rake db:migrate:reset

这将删除您的开发数据库(以及其中的任何数据!),创建一个空的开发数据库,​​然后针对它重新运行所有定义的迁移。

fix your initial migration to have the correct spelling, then run:

$ rake db:migrate:reset

This will delete your development database (and any data in it!), create an empty development db, and re-run all the defined migrations against it.

情绪操控生活 2024-11-23 19:59:37

通常我会回滚迁移(使用 rake db:rollback),修复它并再次运行;

Usually I rollback the migration (with rake db:rollback), fix it and run it again;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文