Search code examples
ruby-on-railsrails-geocoder

Geocoder : ArgumentError: wrong number of arguments (3 for 0..1)


I read many things about this problem but I don't find an issue for the problem. I've created a rake task that find all school in my DB, and find all premium-school. I want to display, thanks to Geocoder, the 3 nearest premiums school on non-premium school. But when I launch my task I have this error :

rake aborted!
ArgumentError: wrong number of arguments (3 for 0..1)
/Users/marchardantonin/.rvm/gems/ruby-2.0.0-p648/gems/origin-2.1.1/lib/origin/selectable.rb:334:in `near'
/Users/marchardantonin/Sites/Vroom2017/vroomvroom-web/lib/tasks/geocodeschool.rake:8:in `block (3 levels) in <top (required)>'
/Users/marchardantonin/.rvm/gems/ruby-2.0.0-p648/gems/mongoid-5.0.0/lib/mongoid/contextual/mongo.rb:668:in `yield_document'

Here is the code :

geocoderschool.rake

namespace :geocodeschool do
  desc "Show premium school near non-premium school and update them"
  task :schgc => :environment do
      @schools = School.all
      @schools_premium = @schools.premium_school
      radius = 30
      @schools.each do |school|
        @schools_aside = @schools_premium.near(school.coordinates.reverse, radius, units: :km).limit(3)
        puts "les auto-écoles premiums : #{@shcools_aside.count}"
        puts "-------"
        @schools_aside.each do |sa|
          puts sa.title
        end
        puts "-------"
    end
  end
end

school.rb

  scope :premium_school, -> {where(:subscription.exists => true).where("subscription.current_period_end" => {'$gte' => Date.today})}

  embeds_one :geocodeschool
  accepts_nested_attributes_for :geocodeschool

geocodeschool.rb

class Geocodeschool
  include Mongoid::Document

  embedded_in :school

  field :school_premium_aside, type: Array

end

Does someone could explain me the error and help me to find an issue ?

Thanks !


Solution

  • Geocode's near method should be called on a model, sending the coordinates.

    So, call it on PremiumSchool model,and send the coordinates in @schools_premium

    @schools_aside = School.near(@schools_premium.coordinates.reverse, radius, units: :km).limit(3)

    Here is the documentation.