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