mongoid-geospatial icon indicating copy to clipboard operation
mongoid-geospatial copied to clipboard

Cannot use within_polygon

Open utkarshdalal opened this issue 8 years ago • 2 comments

Hi, I'm not sure what I am doing wrong, but I am not able to use the within_polygon method. I have a class, Complaint, which is set up like so:

class Complaint
  include Mongoid::Document
  include Mongoid::Geospatial
  include Mongoid::Timestamps::Short
  field :title
  field :description
  field :location, type: Point
  field :images, type: Array
  field :resolved, type: Boolean, default: false
  field :needs_moderation, type: Boolean, default: false
  belongs_to :complaint_type
  belongs_to :user
  has_many :comments
  has_many :likes, as: :feedbackable
  has_many :dislikes, as: :feedbackable
  has_many :moderation_requests

  validates_presence_of :title, :description

  attr_accessor :distance

  spatial_index :location, {min: -200, max: 200}

  spatial_scope :location

  scope :created_after, -> (datetime) {where(:c_at.gte => datetime)}
  scope :created_before, -> (datetime) {where(:c_at.lte => datetime)}
  scope :created_between, -> (start_time, end_time) {created_after(start_time).created_before(end_time)}

When I do Complaint.within_polygon(location: [[[1, 0], [1, 1], [0, 3]]]), I get

NoMethodError: undefined method `within_polygon' for Complaint:Class
Did you mean?  with_options
	from (irb):3
	from /Users/utkarshdalal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
	from /Users/utkarshdalal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
	from /Users/utkarshdalal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
	from /Users/utkarshdalal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
	from /Users/utkarshdalal/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'

What am I doing wrong?

utkarshdalal avatar Mar 22 '17 04:03 utkarshdalal

@utkarshdalal It seems mongoid have some changes of the within_polygon syntax mongoid spec test

class Holiday
  include Mongoid::Document
  include Mongoid::Geospatial

  field :location, type: Point
end

Holiday.create!(location: [1,1])
=> #<Holiday _id: 58f722371064f877a5d1c021, location: [1.0, 1.0]>
Holiday.geo_spacial(:location.within_polygon => [[[0, 0], [0,2], [2, 2], [2, 0], [0, 0]]]).first
=> #<Holiday _id: 58f721771064f877a5d1c020, location: [1.0, 1.0]>

Youngv avatar Apr 19 '17 08:04 Youngv

Someone care to resurrect and figure this one out?

dblock avatar Nov 09 '18 21:11 dblock