简体   繁体   English

在模型实例方法中使用Geokit-Rails时出错

[英]Error while using Geokit-Rails in model instance method

I am trying to use geokit-rails' .within helper method in a model instance method. 我正在尝试在模型实例方法中使用geokit-rails的.within帮助方法。 Take a look here: 在这里看看:

def self.crunch
        users = User.all

        users.each do |user|
            last_movement = Movement.where(user_id: user.id).try(:last)

            if last_movement
                users_near = Movement.where("user_id != ?", user.id).within(5, :origin => last_movement.user_lat, last_movement.user_lng)
            else
                next
            end

            if users_near.first
                users_near.each do |near|
                    #Make sure they don't exist in MB table

                    #Check for interests
                end
            end
        end
    end

I keep getting the error when I try to run the method: "SyntaxError: /Users/arsood/Documents/Consulting/SweetGlue_Service/app/models/matchbox.rb:10: syntax error, unexpected ')'" 当我尝试运行该方法时,我一直收到错误消息:“ SyntaxError:/Users/arsood/Documents/Consulting/SweetGlue_Service/app/models/matchbox.rb:10:语法错误,意外的')'

Line 10 is: 第10行是:

users_near = Movement.where("user_id != ?", user.id).within(5, :origin => last_movement.user_lat, last_movement.user_lng)

When I remove the line it works fine. 当我删除该行时,它工作正常。 Is there a different way I need to call Geokit methods inside of a model rather than a controller? 我需要在模型而不是控制器内调用Geokit方法的其他方法吗?

I think the point is that origin needs a point or an array of lat and lng, you're passing 2 arguments so the synatx is wrong 我认为关键是原点需要点或lat和lng的数组,您传递了2个参数,所以synatx是错误的

This should work without problems 这应该没有问题

users_near = Movement.where("user_id != ?", user.id).within(5, :origin => [last_movement.user_lat, last_movement.user_lng])

Allow me to add a few notes on your code 请允许我在您的代码上添加一些注释

  • where always returns an active record relation, and running last on that would return nil if there's no records, no need to use try where总是返回一个活动的记录关系,如果没有记录, last运行将返回nil,不需要使用try

     Movement.where(user_id: user.id).last 
  • if you are using rails 4, there's a nice not method for active record, so you can change your query to something like this 如果您使用的是Rails 4,则有一个不错的not方法来进行活动记录,因此您可以将查询更改为这样的内容

     Movement.where.not(user_id: user.id).within(5, origin: [last_movement.user_lat, last_movement.user_lng]) 
  • if you add an instance method that returns a point array that would be helpful 如果您添加了一个实例方法,该方法返回的点数组将很有帮助

     class Movement < ActiveRecord::Base def point [ user_lat, user_lng ] end end 

    The query would be simpler 查询会更简单

     users_near = Movement.where.not(user_id:user.id).within(5, origin: last_movement.point) 
  • I'm not sure what the users loop does, but i think it could be improved, if you explain what it's intended to do i could help with that too. 我不确定用户循环的作用,但是我认为它可以改进,如果您解释它的作用,我也可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM