我读了很多关于这个问题的事情,但我找不到问题所在的问题。 我已经创建了一个rake任务,可以查找我的数据库中的所有学校,并找到所有高级学校。感谢Geocoder,这是非高级学校最近的3所保费学校。但是当我启动任务时,我遇到了这个错误:
..[0]
以下是代码:
geocoderschool.rake
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'
school.rb
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
geocodeschool.rb
scope :premium_school, -> {where(:subscription.exists => true).where("subscription.current_period_end" => {'$gte' => Date.today})}
embeds_one :geocodeschool
accepts_nested_attributes_for :geocodeschool
有人能解释我的错误并帮我找到问题吗?
谢谢!
答案 0 :(得分:0)
您应该查看该宝石的official documentations。它只接受散列作为参数,例如:
@schools_premium.near(location: [ 23.1, 12.1 ])
看一下Mongoid code,您似乎应该使用geo_near
:
@schools_premium.geo_near(school.coordinates.reverse).max_distance(0.5)
答案 1 :(得分:0)
应该在模型上调用地理编码的near
方法,发送坐标。
所以,在PremiumSchool
模型上调用它,并在@schools_premium中发送坐标
<强> @schools_aside = School.near(@schools_premium.coordinates.reverse, radius, units: :km).limit(3)
强>