How can I use SQL "IN" statement with Active Record find rails and .map

roles = Role.find_all_by_simulation_id(session[:sim_id])
forum_posts = Post.find(:all, :conditions => ["role_id = ? AND created_at > ?", roles.map(&:id), session[:last_login]])

      

Error:

SQLite3::SQLException: near ",": syntax error: SELECT * FROM "posts" WHERE (role_id = 1,2,3,4 AND created_at > '2009-05-21 11:54:52') 

      

+1


a source to share


2 answers


change this:

forum_posts = Post.find(:all, :conditions => ["role_id = ? AND created_at > ?", roles.map(&:id), session[:last_login]])

      



to

forum_posts = Post.find(:all, :conditions => ["role_id IN (?) AND created_at > ?", roles.map(&:id), session[:last_login]])

      

+2


a source


I think the commas in Role_id = 1,2,3,4. I think it should be role_id = '1,2,3,4' if it is a string or role_id IN (1,2,3,4) if you want to compare OR style with an integer.



+1


a source







All Articles