부울 열이있는 사용자 모델이 있습니다. 기본값은 true
입니다. 레코드를 만들 때 그들은 true
대신 draft_record
: false
으로 생성됩니다. 필드가 초안이라고 불리는 경우이 작업이 효과적 이었지만 초안 연결 및 초안 속성 할당 방법이 충돌하여 초안 속성이 설정 불가능하게되었습니다. 내가 뭔가 잘못하고 있는거야? 나는이 문제를 전에 가지고 있었고 그저 돌아 다니면서 일한 것을 되 돌렸다.레일스 ActiveRecord가 부울 기본값을 준수하지 않습니다
루비 : 1.9.3-p327
레일에루비 : 3.2.12
DBMS : 포스트 그레스
관련 마이그레이션 :
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
저장하기 전에 draft_record를 설정하는 콜백이 없습니다. –