我有两种模式:
class City
include Mongoid::Document
field :alternate_names, type: String
field :coordinates, type: Array
field :name, type: String
index({ coordinates: '2d' }, { min: -180, max: 180 })
belongs_to :country
end
和
class Country
include Mongoid::Document
field :iso_3166_code, type: String
field :name, type: String
has_many :cities
end
在控制器中我使用
@cities = City.where(alternate_names: /#{params[:query].downcase}/).limit(10)
接收城市名单。
以下是每个城市的JSON输出:
...
"country_id": {
"$oid": "56fc453eae3bbe5c2abcd933"
}
...
如何获得country
代替country_id
?
答案 0 :(得分:0)
我找到了解决方案。
@cities = City.where(alternate_names: /#{params[:query].downcase}/).includes(:country).limit(10)
render json: @cities, except: :country_id, include: :country