Geokit query with multiple models involved


My database structure consists of Store, Book, BookStore and Address as below.

class Store < ActiveRecord::Base   has_many :book_stores   has_many :books, :through => :book_stores

  has_many :addresses   acts_as_mappable :through => :addresses end

class Address < ActiveRecord::Base   belongs_to :store   acts_as_mappable end

class Book < ActiveRecord::Base   has_many :book_stores   has_many :stores, :through => :book_stores end

# Join table for many to many relationship between books and stores class BookStore < ActiveRecord::Base   belongs_to :book   belongs_to :store end

Now given a book isbn (which is in the Book model) and a store, I need to find other stores within 5 miles that have that book.

I have looked around but not quite sure how to bring in the Book and BookStore models with my GeoKit query on something like Store.within(5, :origin => @storeaddr).

I am on Rails 3.0.5 and using gem 'geokit-rails3'

Appreciate any thoughts/pointers.


Figured it out

Store.joins(:books, :addresses).where("books.isbn = '9780316346627'").within(5, :origin => @addr)