2013-01-14 97 views
0

我想使用活動記錄來做這個查詢,所以我可以使查詢安全的注入。 DB是Postgresql。 Ruby on Rails的3.2.11有兩個表加入和條件的有效記錄查詢

def find_possible_duplicates(last_name, date_of_birth, organisation_id) 
    find_by_sql('select c.* from clients c' + 
       ' join locations l on c.location_id = l.id' + 
       " where c.last_name ILIKE #{last_name}" + 
       " and c.date_of_birth = #{date_of_birth}" + 
       " and l.organisation_id = #{organisation_id}" 
end   

任何幫助表示讚賞

回答

2

以下是活動記錄查詢:

Client.select('clients.*'). 
joins('join locations on clients.location_id=locations.id'). 
where('last_name like ? and date_of_birth =? and organisation_id = ?',last_name,date_of_birth,organisation_id)