0
我已经在我的TravelIdeas
表字段country_trips
中,其中包含国家/地区数组。Ruby on Rails/Active Record Query:如果字段/记录不包含元素,则返回
#<TravelIdea:0x007faa7bec40f0> {
:id => 9,
:idea_key => "romantic",
:idea_type => nil,
:cost => 2000,
:created_at => Mon, 10 Jul 2017 07:48:49 UTC +00:00,
:updated_at => Mon, 16 Oct 2017 08:10:47 UTC +00:00,
:country_trips => [
[0] "PL"
],
:available_languages => [
[0] "pl"
]
}
地方的旅行在:country_trips
为零,所以我可以忽略它们:
idea.where.not(country_trips: nil) if country.present?
我有我的用户目的地国家,所以我想向他出示所有的旅行理念包含了梦想的国家。
这将是某事像SQL Server包含(列,“国家”)
也许这是不是LIKE
,因为这是不是1个国家匹配1个国家。
例如 country = user.destination_country
手段: country = "PL"
但
:country_trips => [
[0] "PL",
[1] "DE"
],
我在寻找某物像
TravelIdea.where(country_trips: country)
,将返回其拥有这一切旅行的想法计数的国家ry_trips数组:P
难道你使用Rails的''serialize:country_trips',或者你使用RDMS内置系统来存储数组吗? (像这样postgres https://www.postgresql.org/docs/9.1/static/arrays.html) – MrYoshiji
我使用postgres数组 – eudaimonia
试试这个'TravelIdea.where('country_trips = any(?)',' PL')'(你可以谷歌* postgres数组rails *,你会发现像这样的有用信息https://coderwall.com/p/sud9ja/rails-4-the-postgresql-array-data-type) – MrYoshiji