find_or_create_by在belongs_to对象的上下文中调用时没有查找记录的多个属性(2.3.11中的AR错误?)

Tim*_*ter 5 activerecord ruby-on-rails

我正在使用设计添加omniauth到rails 2.3.11应用程序.我基本上都是关注Ryan Bate的railscast(对rails 2.3进行了适当的修改).但是我在测试这部分代码时看到了一些非常奇怪的东西:

class AuthenticationsController < ApplicationController
  ...

  def create
    auth = request.env["omniauth.auth"]
    current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
    flash[:notice] = "Authentication successful."
    redirect_to authentications_url
  end

  ...
end
Run Code Online (Sandbox Code Playgroud)

find_or_create总是创建.在日志中,我看到这个选择:

SELECT * FROM `authentications` WHERE (`authentications`.`provider` IN ('facebook','XXXXXXX') AND `authentications`.`uid` IS NULL) AND ((`authentications`.`user_id` = 10)) LIMIT 1
Run Code Online (Sandbox Code Playgroud)

这不是该方法的正确选择.auth ['provider']和auth ['uid']被正确填充(并且它创建新记录就好了).

更令人困惑的是:如果我进入控制台并且这样做Authentication.find_or_create_by_provider_and_uid('facebook', 'XXXXX'),它可以正常工作(它找到现有的记录).但是,如果我得到一个用户并且这样做user.authentications.find_or_create_by_provider_and_uid('facebook', 'XXXXX'),它会创建一个新记录,我在日志中看到同样有问题的查询语句.

我知道我可以解决这个问题(Ryan Bates后来改变了这段代码),但这非常令人不安.我错过了什么或看起来像ActiveRecord中的错误?

这不是OmniAuth或Devise特有的.在提交之前,我尝试了另外两个类(相当简单的类).相同的结果[Klass.find_or_create_by_a_and_b( 'A', 'B')的工作原理,但parent.klasses.find_or_create_by_a_and_b( 'A', 'B')生成一个选择想要b键为空,一个在( 'A','B ")].

现在我肯定认为这是一个错误2.3.11.在我提交错误之前,有没有人看到我错过的任何内容?有没有人见过这个问题?

Tud*_*tin 0

auth['provider'] 和 auth['uid'] 的值是什么?也许我错了,但在我看来 auth['provider'] 是 ['facebook','XXXXXXX'] 而 auth['uid'] 为零。