You’ll need an ActiveRecord association joining the two models. This might do 
the trick in your User model:

  belongs_to :location, :foreign_key => :zip_code, :primary_key => :zip_code

And then, you’ll want to make sure that join is included in the index 
definition (the `join` call), and then you can refer to the locations table in 
the SQL snippets:

  ThinkingSphinx::Index.define :user, :with => :active_record do
    indexes name, :as => :user, :sortable => true

    has “RADIANS(locations.latitude)”, :as => :latitude, :type => :float
    has “RADIANS(locations.longitude)”, :as => :longitude, :type => :float
    has zip_code, :type => :integer
    
    join location
  end

Give that a spin, see how you go.

Cheers

— 
Pat

On 30 Oct 2014, at 8:29 am, [email protected] wrote:

> I need to covert degrees to radians for both the User and Locations index. 
> The conversion is working beautifully for Location index. The trouble I am 
> having is with the User index. Since the User model does not store latitude 
> and longitude, I created a relationship between the two tables in the User 
> model by doing:
> 
>   def latitude
>       location = Location.find_by_zip_code(zip_code)
>       if location
>         location.latitude
>     end
>   end 
> 
> Below is the location and user index. I have this error when I perform a 
> search "index user_core: parse error: Sphinx expr: syntax error, unexpected 
> TOK_IDENT near 'latitude, longitude)'". The error disappears when I remove 
> the conversion from the Users index, and instead I get `undefined method 
> `inject' for nil:NilClass`. Which I figured inject error is due to not having 
> the conversion setup.
> 
> ThinkingSphinx::Index.define :location, :with => :active_record do 
>   indexes city 
>   
>   has "RADIANS(locations.latitude)",  :as => :latitude,  :type => :float
>   has "RADIANS(locations.longitude)", :as => :longitude, :type => :float
> end 
> ThinkingSphinx::Index.define :user, :with => :active_record do 
>   indexes name, :as => :user, :sortable => true 
>   has "RADIANS(User.locations.latitude)",  :as => :latitude,  :type => :float
>   has "RADIANS(User.locations.longitude)", :as => :longitude, :type => :float
>   has(:zip_code, :as => :zip_code, :type => :integer)
> end 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/thinking-sphinx.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/d/optout.

Reply via email to