5

I am using Sunspot for searching and using Geocoder for addresses and then for calculating distances, Geokit-rails3.

class Product < ActiveRecord::Base
  belongs_to :store
  searchable do
    text :name
  end 
end

class Store < ActiveRecord::Base
  acts_as_mappable
  geocoded_by :address, :latitude => :lat, :longitude => :lng
  attr_accessible :lat, :lng, :address
  has_many :products
end

Question

What I want when typing in a product to search for is also the ability to type an address inside of another field to search for products in that given area with a 30 mile radius.

This is my controller which allows me to search for Products:

class SearchController < ApplicationController

  def index
    @search = Product.search do |q|
      q.fulltext params[:search]
    end
    @products = @search.results
  end
end

So I believe the form would look something like this after I am done:

<%= form_tag search_path, :method => 'get' do %>
        <%= text_field_tag :search, params[:search]" %>
        <%= submit_tag "Search", :name => nil %>
        <p> Search Near: </p>
        <%= label_tag :location, "Search nearby an Address" %>
        <%= text_field_tag :location, params[:location] %>
<% end %>

I am thinking :location would serve as a virtual attribute for the Stores :address in order for the field to be mapped correctly but this is all speculation.

How do I set this all up in order to achieve the specific scenario?

LearningRoR
  • 26,582
  • 22
  • 85
  • 150
  • Be sure to checkout [this RailsCast](http://railscasts.com/episodes/273-geocoder). I think it exactly answers your question. – Mario Uher Nov 22 '11 at 12:05
  • I believe this thread can help you out: https://groups.google.com/group/ruby-sunspot/browse_frm/thread/ecc8231569ea7ef7/f8569abee2197a19?lnk=gst&q=geokit#f8569abee2197a19 – Jryl Dec 08 '11 at 00:09

0 Answers0