我有一个用户模型draft_record
布尔列,其默认为true
。在创建记录时,它们使用draft_record
:false
而不是true
创建。当该字段被称为草稿时,这起作用,然而,草稿关联和草稿属性分配方法发生冲突,导致草稿属性不可设置。难道我做错了什么?之前我遇到过这个问题,只是通过恢复工作来解决这个问题。轨道ActiveRecord不尊重布尔默认
红宝石:1.9.3-P327
Ruby on Rails的:3.2.12
DBMS:Postgres的
相关迁移:
class AddDraftColumnToUsers < ActiveRecord::Migration
def self.up
add_column :users, :draft_record, :boolean, default: true
add_column :users, :draft_id, :integer
add_column :users, :current_id, :integer
end
def self.down
...
end
end
合力架构
ActiveRecord::Schema.define(:version => 20130303002123) do
create_table "users", :force => true do |t|
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name"
t.boolean "draft_record", :default => true
t.integer "draft_id"
t.integer "current_id"
end
end
创建用户对象:
Loading development environment (Rails 3.2.12)
1.9.3-p327 :001 > u = User.create(name: "Jon")
(0.0ms) begin transaction
SQL (28.8ms) INSERT INTO "users" ("created_at", "current_id", "draft_id", "draft_record", "name", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Sun, 03 Mar 2013 00:42:04 UTC +00:00], ["current_id", nil], ["draft_id", nil], ["draft_record", false], ["name", "Jon"], ["updated_at", Sun, 03 Mar 2013 00:42:04 UTC +00:00]]
(0.7ms) commit transaction
=> #<User id: 1, created_at: "2013-03-03 00:42:04", updated_at: "2013-03-03 00:42:04", name: "Jon", draft_record: false, draft_id: nil, current_id: nil>
1.9.3-p327 :002 >
您是否有任何回调函数将草稿记录保存到数据库之前? – jvnill 2013-03-03 03:10:05
保存前没有设置draft_record的回调。 – 2013-03-03 10:11:19