2012-06-07 52 views
0

嗨,我正在使用rails和活動記錄,並且我的活動記錄查詢存在一些問題。ActiveRecord中的SQL錯誤

session[:profile_ids] = [2,4,5] 
@profiles = UserProfile.where("key = ? and id in (?)", 'Noc_Address', 
    session[:profile_ids]) 

但我收到此錯誤:

Mysql::Error: You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near 
'key = 'Noc_Address')' at line 1: SELECT `user_profiles`.* FROM 
`user_profiles` WHERE (id in (361) and key = 'Noc_Address') 

我如何糾正呢?

謝謝。

回答

4

KEYreserved word所以你要逃避它:

@profiles = UserProfile.where("`key` = ? and id in (?)", 'Noc_Address', session[:profile_ids]) 

還是讓Rails的您處理該問題:

@profiles = UserProfile.where(key: 'Noc_Address', id: session[:profile_ids])