2012-06-07 4 views
0

Привет, я использую рельсы и активную запись, и есть некоторая проблема с моим активным запросом записи.Ошибка SQL в ActiveRecord

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

KEY является reserved word, так что вы должны избежать этого:

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

Или пусть Rails справиться с этим для вас:

@profiles = UserProfile.where(key: 'Noc_Address', id: session[:profile_ids]) 
Смежные вопросы