Jas*_*nis 1 database validation activerecord model ruby-on-rails
我的模型看起来像这样,没什么特别的:
# Table name: invoices
#
# id :integer not null, primary key
# total :float
# discount :float
# description :text
# created_at :datetime not null
# updated_at :datetime not null
# company_id :integer
# secret :string(255)
# address_id :integer
# price_per_credit :float
# paid :boolean
Run Code Online (Sandbox Code Playgroud)
我可以在服务器和rails控制台上创建这两个实例,如果我调用.valid?它们,这些实例将返回true (为了测试目的,我关闭了所有验证).
1.9.3p194 :001 > i = Invoice.new
=> #<Invoice id: nil, total: nil, discount: nil, description: nil, created_at: nil, updated_at: nil, company_id: nil, secret: nil, address_id: nil, price_per_credit: nil, paid: nil>
1.9.3p194 :002 > i.valid?
=> true
Run Code Online (Sandbox Code Playgroud)
但是,由于某种原因,我无法将这些实例保存到数据库中.
1.9.3p194 :003 > i.save
(0.3ms) begin transaction
(0.1ms) rollback transaction
=> false
Run Code Online (Sandbox Code Playgroud)
本地我正在使用sqlite,生产服务器正在运行MySQL,但两者都显示了这种行为.我真的不知道如何调试这个.我跑了db:migrate,我的宝石是最新的.我怎么能这样呢?
事实证明我有一个before_create返回false 的过滤器,导致保存过程停止.为了解决这个问题,我nil在课堂上添加了这样的内容:
# BEFORE:
def set_paid
self.paid = false
end
# AFTER:
def set_paid
self.paid = false
nil
end
Run Code Online (Sandbox Code Playgroud)
希望这也有助于其他人!
| 归档时间: |
|
| 查看次数: |
466 次 |
| 最近记录: |