-
Hi, Following is a example. I have a SysConfig class like this: class SysConfig < BaseModel
table do
column private_key : String
column deleted_at : Time?
end
def self.current
CreateSysConfig.create!(private_key: "", deleted_at: nil)
end
end I use ActiveRecord migration for create this table, following is migration. class CreateSysConfigs < ActiveRecord::Migration[7.0]
def change
create_table :sys_configs, comment: '系统配置' do |t|
t.text :private_key, comment: '私钥'
t.datetime :deleted_at, comment: '删除时间'
t.timestamps
end
end
end Following is my CreateSysConfig: class CreateSysConfig < SysConfig::SaveOperation
permit_columns private_key, deleted_at
end The Weired thing is, when i run
My question is, why private_key column couldn't accept a empty string "" as value? i could see anywhere have limit for it, in fact, i can even create a new order from rails console all column is nil. irb(main):002:0> SysConfig.create
TRANSACTION (0.1ms) BEGIN
SysConfig Create (0.5ms) INSERT INTO "sys_configs" ("private_key", "deleted_at", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["private_key", "[FILTERED]"], ["deleted_at", nil], ["created_at", "2022-07-11 09:04:02.567781"], ["updated_at", "2022-07-11 09:04:02.567781"]]
TRANSACTION (5.4ms) COMMIT
=>
#<SysConfig:0x00007fd44f3777f8
id: 3,
private_key: nil,
deleted_at: nil,
created_at: Mon, 11 Jul 2022 09:04:02.567781000 UTC +00:00,
updated_at: Mon, 11 Jul 2022 09:04:02.567781000 UTC +00:00> after the above current method change to: CreateSysConfig.create!(private_key: "hello", deleted_at: nil), it works, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
In Lucky, we treat We have 2 ways around this: class CreateSysConfig < SysConfig::SaveOperation
permit_columns private_key, deleted_at
# add this to skip validating the required fields
skip_default_validations
end or class CreateSysConfig < SysConfig::SaveOperation
permit_columns private_key, deleted_at
before_save do
# allow the private_key to be blank
private_key.allow_blank = true
end
end I think we're missing docs on these, but here's the PR luckyframework/avram#746 |
Beta Was this translation helpful? Give feedback.
In Lucky, we treat
""
the same asnil
because this helps with additional type-safety. For example, say you have aname
column that gets displayed on a page. If it got set to""
, the page would render a blank space, and you wouldn't know until production.We have 2 ways around this:
or
2.
I think we're missing docs on these, …