簡體   English   中英

創建新用戶使用ruby 1.9.2,但無法使用ruby 1.9.3

[英]Create new user works with ruby 1.9.2 but fails with ruby 1.9.3

我正在使用Rails 3.2.8和Devise 2.1.2(最新),並且對為什么我可以使用完全相同的代碼庫創建用戶使用Ruby 1.9.2很好,但不能使用Ruby 1.9.3感到困惑。

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:驗證失敗...”錯誤。 1.9.2沒有錯誤。

如果我將其設置為10而不是9.95,則它可以在1.9.2和1.9.3中使用(經紀人佣金設置為10)。

如果將其設置為9,95(逗號,而不是句點),則可以很好地創建用戶和代理,但將代理佣金設置為0。

如驗證錯誤消息所述,不允許使用句點(。),因此驗證失敗是有道理的,但是在我切換到1.9.3之前,這可能是ruby 1.9.2中的一個錯誤,它可以正常工作。

任何好的解釋都將受到歡迎:-)

保存失敗后,檢查@ 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM