Ruby on rails搜索数组(活动记录)

时间:2016-02-13 06:01:06

标签: ruby-on-rails ruby postgresql activerecord rails-activerecord

我有一个带有数组列的模型。所以我基本上将语言作为数组保存到Postgres。例如,driver_language =["English", "Spanish", "French"]

在我的查询中,我想选择driver_language中包含submitted_driver_language ["English", "Spanish"]的所有车辆。或者,提交的driver_language数组中至少有一个元素包含在driver_language列中,该列是一个数组。如何将该条件添加到下面的查询中?任何帮助将不胜感激

Vehicle.where(:vehicle_type => vehicle_type, :active => true, :company_activated => true, :capacity => number_of_people.to_i..Float::INFINITY)

2 个答案:

答案 0 :(得分:0)

使用arel_table。

@vehicle = Vehicle.where(:vehicle_type => vehicle_type, :active => true, :company_activated => true, :capacity => number_of_people.to_i..Float::INFINITY)

avehicle = @vehicle.arel_table

@required_result = @vehicle.where(avehicle[:driver_language].matches('%#{"English", "Spanish"}%'))

答案 1 :(得分:0)

正则表达式怎么样?

Vehicle.where('driver_language ~* ?', 'english|spanish')