具有两个表的连接和条件的活动记录查询

时间:2013-01-14 06:12:54

标签: ruby-on-rails-3 activerecord

我想使用活动记录执行此查询,以便我可以从注入中安全地查询。 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        

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:2)

以下是Active Record查询:

 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)
相关问题