Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:15:26+00:00 2026-05-27T15:15:26+00:00

I’m using the Geocoder and Sunspot gem in my application and I have a

  • 0

I’m using the Geocoder and Sunspot gem in my application and I have a field called :search_near_address that is suppose to give users the ability to input an address they want to search near within X amount of miles. What I am trying to map to is my Stores :address to use for the :search_near_address field. This way users can input an address ( i.e. 451 University Avenue, Palo Alto, CA ) in the :search_near_address field and it will search in a radius of 50 miles.

Answer

Sunspot 1.2.1


class Store < ActiveRecord::Base
   attr_accessible :address, :latitude, :longitude
   has_many :products
   geocoded_by :address
   after_validation :geocode
   reverse_geocoded_by :latitude, :longitude
   after_validation :reverse_geocode      
end

class Product < ActiveRecord::Base
   belongs_to :store

   searchable do # Searching with product model.
     string :search_near # For rake sunspot:reindex
     location :location
   end

   def search_near_address
 store.address if store # You may have to use the "if store".
   end

   def location
    # may need the "if store" after longitude)....
Sunspot::Util::Coordinates.new(store.latitude, store.longitude)
   end
end

class SearchController < ApplicationController

  def index
    @search = Product.search do |q| # Search with sunspot
      q.fulltext params[:search]
      q.with(:location).near(*Geocoder.coordinates(params[:search_near_address]), :precision => 4) if params[:search_near_address].present?
    end 
    @products = @search.results # Return results from Product.search block.
  end
end

# search/index/html.erb
<%= form_tag results_search_index_path, :method => 'get' do %>
    <%= text_field_tag :search, params[:search] %>
    <%= text_field_tag :search_near_address, params[:search_near_address] %>
    <%= submit_tag "Go", :name => nil %>
<% end %>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:15:27+00:00Added an answer on May 27, 2026 at 3:15 pm

    First what @m_x said is right, you can’t expect assigning the Store#near results to the @search object would work….

    Just reading the docs about Geospatial in https://github.com/sunspot/sunspot, you can clearly see that you are missing a few things:

    You have to declare your Geospatial field using the latitude and longitude fields that geocoder requires:

    class Product < ActiveRecord::Base
       belongs_to :store
    
       searchable do # for Sunspot search.
         string :search_near
         latlon(:location) { 
           Sunspot::Util::Coordinates.new(category.latitude, category.longitude)
         }
       end
    
       def search_near
         #I changed this because business_store will not work out.
         store.address
       end
    end
    

    Then you can search (after having indexed something) like this:

    class SearchController < ApplicationController
    
      def index
        # Search with sunspot
        @search = Sunspot.search(Product) do
          fulltext params[:search]
    
          # The "*" pop off the elements of the array that 
          # Geocoder.coordinates returns.
          with(:location).near(*Geocoder.coordinates(params[:search_near]), 
                               :precision => 6)
    
          # NOTE: You could also use the in_radius method but only in the pre-release version:
          # with(:location).in_radius(*Geocoder.coordinates(params[:search_near]), 100) if params[:search_near].present?
        end
    
        # Return results from Product.search block.
        @products = @search.results 
      end
    end
    

    Also read about the :precision option here http://sunspot.github.com/docs/Sunspot/DSL/RestrictionWithNear.html#near-instance_method

    About Geocoder https://github.com/alexreisner/geocoder

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.