2011-01-08 46 views
0

是什麼軌之間的區別構建:ActiveRecord的 - find_by V/S,其中

authentication = Authentication.where(:provider=>omniauth['provider'], :uid=>omniauth['uid']) 
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 

我的問題是,如果我使用的第一構造(。凡) - 我無法得到的關聯。我的模型身份驗證屬於'模型用戶。如果我使用first構造搜索身份驗證,則無法通過authentication.user獲取關聯的用戶,但同樣的事情可以與第二個版本一起使用。

由於Heroku問題,我想使用第一個構造而不是第二個構造: http://docs.heroku.com/database:PGError:錯誤:運算符不存在:字符變化=整數。

+0

我正在使用heroku,第二個工作正常。你的提供者和uid都是字符串類型嗎? – PeterWong 2011-01-08 17:08:46

+0

uid是varchar,但是當它通過數字進行比較時,我從Postgres得到錯誤。我正在使用omniauth身份驗證方法。不知道如何解決這個 – Ved 2011-01-08 17:13:05

回答

1

PGError: ERROR: operator does not exist: character varying = integer.

這不是英雄問題。 Postgresql自8.4或8.3以來引入了更多限制性的類型檢查。例如,如果你的列「position」是整數,你不能寫這個sql:

select * from orders where position = '1'; 

它會返回類似於你的錯誤。

你可以嘗試:從意見

auth = Authentication.find :all, :conditions => ["provider = ? and uid = ?", provider, uid] 
1

轉播不錯的答案:

也許呼籲omniauth .to_s [ 'UID'],所以軌道認爲這是字符串,並將它傳遞給Postgres的爲varchar?