简体   繁体   中英

ActiveRecord - find_by v/s where

What is the difference between rails constructs:

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

My problems is that if I use first construct (.where) - I am unable to get the association. My model Authentication 'belongs to' model user. If I search for the Authentication using first construct, I am not able to get associated User via : authentication.user, but the same thing works with the second version.

I want to use the first construct instead of the second one because of Heroku issue : http://docs.heroku.com/database : PGError: ERROR: operator does not exist: character varying = integer.

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

It's not heroku issue. Postgresql since 8.4 or 8.3 introduced more restrictive type checking. For example if your column "position" is integer you can't write this sql:

select * from orders where position = '1';

It will return error similar to yours.

You could try:

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

Repost good answer from comments:

maybe call .to_s on omniauth['uid'], so rails think it's string and pass it to postgres as varchar?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM