【问题标题】:Geokit-Rails: polymorphic location queryGeokit-Rails:多态位置查询
【发布时间】:2014-03-20 13:46:55
【问题描述】:

Location 有 :lat 和 :lng 用于 'geokit-rails' 我正在运行 Rails 4。

是否可以查询在指定位置范围内具有 Track 的不同集合?

class Location < ActiveRecord::Base
  belongs_to :locatable, polymorphic: true
  acts_as_mappable
end

class Tracks < ActiveRecord::Base
has_one :location, as: :locatable
belongs_to :collection
end

class Collection < ActiveRecord::Base
  belongs_to :user
  has_many :tracks
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 geolocation polymorphism geokit


    【解决方案1】:

    要在 grokit-rails 中使用多态,您必须通过: :location 将acts_as_mappable 添加到您的 Tracks 模型中:

    class Tracks < ActiveRecord::Base
      has_one :location, as: :locatable
      acts_as_mappable through: :location
      belongs_to :collection
    end
    

    而且查询必须是:

    @collection.tracks.joins(:location).within(distance, :origin => @origin)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多