2012-10-10 99 views
0

我正在使用Rails 3.2.8和Devise 2.1.2(最新),并为我为什么可以创建一个用户很好用Ruby 1.9 .2但不能与Ruby 1.9.3,使用完全相同的代码库。创建新用户使用红宝石1.9.2但失败与红宝石1.9.3

Rails的控制台:

User.find_or_create_by_email('[email protected]', :password => 'example', :first_name => 'Super', :last_name => 'Admin', :terms_and_conditions => true) 

的输出1.9.3显示ROLLBACK(见下文),但我似乎无法找出原因。使用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 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.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 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: "[email protected]", ... 
(stops) 

我想这可能是与设计的问题,但我不确定。

将不胜感激任何帮助!


更新10月19日:

我发现这个问题的原因是after_create回调user.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中正常工作,但是无法使用红宝石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.2之前,它已经有了一个bug,在我切换到1.9.3之前它已经工作了。

欢迎任何好的解释:-)

+0

你确定这是不是一个验证问题? INSERT看起来几乎相同。 – tadman

+0

@tadman谢谢 - 但代码运行在相同的代码库上,那么它怎么会是一个验证问题? – rassom

+1

这可能是你的'before_save'类型例程中的一个无意中返回'false'并暂停保存操作,或者像是失败的唯一性检查。确保你使用'save!'或'create!'来正确触发异常。 – tadman

回答

0

保存失败后,请检查@user。错误它可能有一个线索

编辑

在Rails控制台

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

@user.errors.full_messages 
+0

它可以在不与模型混淆的情况下完成吗?如果是的话,我怎么能在控制台上做到这一点? (新手Rails,所以希望你能和我一起袒护) – rassom

+0

'createuser.errors.full_messages'给你很好的,可读的输出,如果有任何错误。 – tadman

+0

我编辑了我的原始答案,请再次检查 – graudeejs