【问题标题】:Create new user works with ruby 1.9.2 but fails with ruby 1.9.3创建新用户适用于 ruby​​ 1.9.2,但使用 ruby​​ 1.9.3 失败
【发布时间】: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 =&gt; 'example', :first_name =&gt; 'Super', :last_name =&gt; 'Admin', :terms_and_conditions =&gt; true)createuser.save! 但没有出现错误/异常(ROLLBACK 已经出现在步骤 1 中,即在 createuser.save 之前!)。你会这样做吗? (我是 Rails 新手,所以请与我裸露)评论只能编辑 5 分钟(单击此框可关闭)
  • 大家好。我已经找出导致问题的原因。请查看更新后的问题。

标签: ruby-on-rails ruby ruby-on-rails-3 devise


【解决方案1】:

保存检查失败后@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

【讨论】:

  • 可以不弄乱模型吗?如果是,我该如何在控制台中做到这一点? (Rails 的新手,所以希望你能和我一起裸露)
  • 如果有任何错误,createuser.errors.full_messages 会为您提供漂亮、可读的输出。
  • 谢谢,伙计们。试过但没有显示错误消息:-/当我运行@user = User.find_or_create_by_email(...(仍然得到回滚输出)然后在控制台中运行@user.errors.full_messages时,我只得到=&gt; []任何其他想法?
  • 你检查过这个用户存在吗?
猜你喜欢
  • 1970-01-01
  • 2011-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 2021-07-13
  • 2014-03-03
  • 1970-01-01
相关资源
最近更新 更多