change activerecord code to sql statement