【发布时间】:2012-10-10 14:14:43
【问题描述】:
我正在使用 Rails 3.2.8 和 Devise 2.1.2(最新),我很困惑为什么我可以使用 Ruby 1.9.2 而不是使用 Ruby 1.9.3 创建一个用户,使用 exactly 相同的代码库。
Rails 控制台:
User.find_or_create_by_email('example@example.com', :password => 'example', :first_name => 'Super', :last_name => 'Admin', :terms_and_conditions => true)
1.9.3 中的输出显示 ROLLBACK(见下文),但我似乎无法弄清楚原因。
使用 1.9.2 时的输出(部分):
SQL (0.3ms) INSERT INTO "versions" ("created_at", "event", "item_id", "item_type", "object", "whodunnit") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Wed, 10 Oct 2012 13:57:07 UTC +00:00], ["event", "create"], ["item_id", 20], ["item_type", "OrderType"], ["object", nil], ["whodunnit", nil]]
Currency Load (1.1ms) SELECT "currencies".* FROM "currencies" WHERE "currencies"."code" = 'USD' LIMIT 1
SQL (2.4ms) INSERT INTO ..
(continues with the next insert statement)
使用 1.9.3 时的输出(部分):
SQL (0.3ms) INSERT INTO "versions" ("created_at", "event", "item_id", "item_type", "object", "whodunnit") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Wed, 10 Oct 2012 12:29:49 UTC +00:00], ["event", "create"], ["item_id", 16], ["item_type", "OrderType"], ["object", nil], ["whodunnit", nil]]
Currency Load (0.3ms) SELECT "currencies".* FROM "currencies" WHERE "currencies"."code" = 'USD' LIMIT 1
(0.1ms) ROLLBACK
=> #<User id: 4, first_name: "Super", last_name: "Admin", admin_notes: nil, email: "example@example.com", ...
(stops)
我认为这可能是 Devise 的问题,但不确定。
不胜感激!
10 月 19 日更新:
我发现问题的原因是 user.rb 中的 after_create 回调创建了一个“经纪人”(通过经纪人模型),它设置佣金 = 9.95
来自 broker.rb 中的注解:
# commission :decimal(, )
更多来自 broker.rb:
belongs_to :user
PRICE_REGEX = /^([1-9]\d{0,20}|0)(\.\d{0,3})?$/
PRICE_ERROR_MESSAGE = "Must be a number. If you want to include decimals, please use comma as decimal separator. Periods (.) = thousand separator cannot be used."
validates :commission, :format => {:with => PRICE_REGEX, :message => PRICE_ERROR_MESSAGE }, :allow_blank => true
如上所述,将经纪人佣金设置为 9.95 在 ruby 1.9.2 上可以正常工作,但在 ruby 1.9.3 上会失败。
在 1.9.3 中通过 rails 控制台保存它时,我收到“ActiveRecord::RecordInvalid: Validation failed...”错误。 1.9.2没有错误。
如果我将其设置为 10 而不是 9.95,它在 1.9.2 和 1.9.3 中都可以使用(经纪人佣金设置为 10)。
如果我将其设置为 9,95(逗号,而不是句点),则用户和经纪人创建良好,但经纪人佣金设置为 0。
正如验证错误消息所说,句点 (.) 是不允许的,因此验证失败是有道理的,但是在我切换到 1.9.3 之前它会是 ruby 1.9.2 中的一个错误,它可以工作.
欢迎任何好的解释:-)
【问题讨论】:
-
您确定这不是验证问题吗?
INSERT看起来几乎一样。 -
@tadman 谢谢 - 但是代码在同一个代码库上运行,所以它怎么可能是一个验证问题?
-
可能是您的
before_save类型例程之一无意中返回了false并停止了保存操作,或者存在诸如唯一性检查失败之类的事情。确保您使用save!或create!正确触发异常。 -
我在 Rails 控制台中尝试了以下操作:
createuser =User.find_or_create_by_email('example@example.com', :password => 'example', :first_name => 'Super', :last_name => 'Admin', :terms_and_conditions => true)createuser.save!但没有出现错误/异常(ROLLBACK 已经出现在步骤 1 中,即在 createuser.save 之前!)。你会这样做吗? (我是 Rails 新手,所以请与我裸露)评论只能编辑 5 分钟(单击此框可关闭) -
大家好。我已经找出导致问题的原因。请查看更新后的问题。
标签: ruby-on-rails ruby ruby-on-rails-3 devise