Rails 未添加模型字段来插入查询

发布于 2024-09-12 22:42:00 字数 1091 浏览 3 评论 0原文

我通过迁移向 Rails 中的数据库添加了一列,并更新了 new.html.erb 以将该字段添加到表单中。

请求参数中包含正确的数据,但生成的插入查询每次该列的值为空。

这是请求参数:

{"commit"=>"Create",
  "assignment"=>{"start_date"=>"08/04/2010",
  "project_id"=>"5",
  "allotment"=>"50",
  "person_id"=>"33",
  "end_date"=>"08/05/2010"},
  "authenticity_token"=>"8f157a2a220caf716607162ce9557ab6505aab1a"}

这是显示空列的查询所产生的错误:

Mysql::Error: Column 'end_date' cannot be null: 
INSERT INTO `assignments` (`start_date`, `created_at`, `project_id`, `updated_at`, `allotment`, `person_id`, `end_date`)
VALUES('2010-08-04', '2010-08-04 03:36:21', 5, '2010-08-04 03:36:21', 50, 33, NULL)

这是有问题的迁移:

class ChangeDates < ActiveRecord::Migration
  def self.up
    add_column :assignments, :end_date, :date, :null => false
    rename_column :assignments, :date, :start_date
  end

  def self.down
    remove_column :assignments, :end_date
    rename_column :assignments, :start_date, :date
  end
end

我无法弄清楚出了什么问题。我已经回滚并迁移了几次更改,但没有成功。我很困惑。

I've added a column to a db in rails via a migration, and updated new.html.erb to add the field to the form.

The correct data is in the request params, but the insert query that is generated has a null for the column every time.

Here are the request params:

{"commit"=>"Create",
  "assignment"=>{"start_date"=>"08/04/2010",
  "project_id"=>"5",
  "allotment"=>"50",
  "person_id"=>"33",
  "end_date"=>"08/05/2010"},
  "authenticity_token"=>"8f157a2a220caf716607162ce9557ab6505aab1a"}

and here is the resulting error with query showing the null column:

Mysql::Error: Column 'end_date' cannot be null: 
INSERT INTO `assignments` (`start_date`, `created_at`, `project_id`, `updated_at`, `allotment`, `person_id`, `end_date`)
VALUES('2010-08-04', '2010-08-04 03:36:21', 5, '2010-08-04 03:36:21', 50, 33, NULL)

Here's the migration in question:

class ChangeDates < ActiveRecord::Migration
  def self.up
    add_column :assignments, :end_date, :date, :null => false
    rename_column :assignments, :date, :start_date
  end

  def self.down
    remove_column :assignments, :end_date
    rename_column :assignments, :start_date, :date
  end
end

I can't figure out what went wrong. I've rolled back and migrated the change a couple of times with no luck. I'm stumped.

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

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

发布评论

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

评论(2

御守 2024-09-19 22:42:00

为了回答有关 attr_accessor 的问题,范围将在类方法之前调用访问器方法,因此将值保存到访问器,但是当调用保存时,它返回方法调用。我希望通过示例来澄清这一点: http://codepad.org/gAppUMc4

我也做了同样的事情几次。其超级令人沮丧。

To answer you question about attr_accessor, scope will call the accessors method before the class method, so were saving the value to the accessor, but when the save was called it was returning the method call. I hope this clarifies it by example: http://codepad.org/gAppUMc4

I've done the same thing several times. Its super frustrating.

把人绕傻吧 2024-09-19 22:42:00

如果您还没有这样做,请尝试重新启动您的网络服务器。 Rails 可能会缓存列名称。

您正在运行什么版本的 Rails 以及什么网络服务器?

If you haven't already, try restarting your webserver. Rails might be caching the column names.

What version of Rails and what webserver are you running?

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