0

我正在使用 Rails 3.2.8 和 Devise 2.1.2(最新),我很困惑为什么我可以使用完全相同的代码库使用 Ruby 1.9.2 但不能使用 Ruby 1.9.3 创建用户。

导轨控制台:

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 日更新:

我发现问题的原因是after_createuser.rb 中的回调创建了一个“经纪人”(通过经纪人模型),它设置佣金 = 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 中的一个错误,它可以工作。

欢迎任何好的解释:-)

4

1 回答 1

0

保存检查失败后@user.errors 它可能有一个线索

编辑

在你的 Rails 控制台中

@user = User.find_or_create_by_email('example@example.com', :password => 'example', :first_name => 'Super', :last_name => 'Admin', :terms_and_conditions => true)
@user.errors

或者

@user.errors.full_messages
于 2012-10-10T15:49:40.207 回答