Sql 在Rails上应用联接和合并后缺少属性

Sql 在Rails上应用联接和合并后缺少属性,sql,ruby-on-rails,sqlite,inner-join,rails-geocoder,Sql,Ruby On Rails,Sqlite,Inner Join,Rails Geocoder,我有两个模型 Category has_many :locations Location belongs_to :category reverse_geocoded_by :latitude, :longitude do |obj,results| if geo = results.first obj.address = geo.address obj.city = geo.city obj.country = geo.country_code end e

我有两个模型

Category

has_many :locations

Location

belongs_to :category
reverse_geocoded_by :latitude, :longitude do |obj,results|
 if geo = results.first
    obj.address = geo.address
    obj.city    = geo.city
    obj.country = geo.country_code
 end
end
after_validation :reverse_geocode
当我申请时,获取该位置最近的类别。我在geocoder gem中使用了近范围

Category.joins(:locations).merge(Location.near("Tignale"))
在SQL中

    SELECT locations.*, (69.09332411348201 * ABS(locations.latitude - 45.7426301) * 0.7071067811865475) + (59.836573914187355 * ABS(locations.longitude - 10.7091062) * 0.7071067811865475) AS distance, CASE WHEN (locations.latitude >= 45.7426301 AND locations.longitude >= 10.7091062) THEN  45.0 WHEN (locations.latitude <  45.7426301 AND locations.longitude >= 10.7091062) THEN 135.0 WHEN (locations.latitude <  45.7426301 AND locations.longitude <  10.7091062) THEN 225.0 WHEN (locations.latitude >= 45.7426301 AND locations.longitude <  10.7091062) THEN 315.0 END AS bearing FROM "categories" INNER JOIN "locations" ON "locations"."category_id" =
 "categories"."id" WHERE (locations.latitude BETWEEN 45.4531665337783 AND 46.0320936662217 AND locations.longitude BETWEEN 10.29433218237017 AND 11.123880217629832)  ORDER BY distance ASC
我只知道这个

[#<Category:0xb090be0
  id: 3,
  created_at: Thu, 03 Sep 2015 21:46:12 UTC +00:00,
  updated_at: Sat, 05 Sep 2015 21:36:19 UTC +00:00>]

类别模型上缺少一些其他属性,例如“名称”等。

生成的SQL是什么?看起来是因为地理编码器的作用域。如果你输入一个.select来明确表示你想要分类列,会发生什么?当我像category.joins:locations.mergeLocation.nearTignale,20岁,select:categories.*。非常感谢@japed​选择:类别。*为我修复了它!