Hi Pat,

I'm surprised to hear this isn't quite a common scenario. Applying the
logic to other similar models, for example Products and Stores. A
Store has a longitude and latitude, and the store has many Products.
Equally a Product can exist in many Stores. If you wanted to find
'widgets' in stores in the London area, wouldn't this be modelled in a
similar way to my models described above?

Somehow I think I must be missing something, if this isn't a scenario
you've seen before.

As always, any insights would be greatly appreciated.

Thanks again.
Alex


On Jan 24, 11:56 pm, Pat Allan <[email protected]> wrote:
> Oh, it's a many-to-many... I didn't spot that the first time around. That 
> does make things more complicated. I've not seen this scenario before.
>
> To be honest, I can't think of a neat solution at the moment - you could run 
> a search per site using the same params but just for promotions, to get the 
> relevant promotion per site, but the performance on that would not be 
> ideal... and then you'll hit situations where for some sites you'll get more 
> than one promotion matching.
>
> Sorry Alex, I can't think of an easy way to do this.
>
> --
> Pat
>
> On 25/01/2011, at 7:14 AM, Alex wrote:
>
>
>
> > Hi Pat,
>
> > Thank you for replying so promptly, it's really appreciated.
>
> > As advised, I've added an index to the Site model, like so:
>
> > class Site < ActiveRecord::Base
> >  acts_as_mappable
>
> >  # associations
> >  has_many :campaigns, :dependent => :destroy
> >  has_many :promotions, :through => :campaigns
>
> >  define_index do
> >    indexes promotions.title, :as => :promotions_title
> >    indexes promotions.details, :as => :promotions_details
> >    has promotions(:id), :as => :promotions_ids
> >    has 'RADIANS(sites.lat)', :as => :lat, :type => :float
> >    has 'RADIANS(sites.lng)', :as => :lng, :type => :float
> >    set_property :latitude_attr   => "lat"
> >    set_property :longitude_attr  => "lng"
> >  end
> > end
>
> > Then in my controller I do a Site.search 'widgets', :geo => [@lat,
> > @lng], :with => {"@geodist" => 0.0..10}
>
> > Now, here is where I get stuck. Sorry for the newb question....
>
> > That returns me an array of Sites. How do I get to the Promotions that
> > are associated with those sites in my views?
>
> > Thank you again for all your help.
> > Alex
>
> > On Jan 24, 1:51 pm, Pat Allan <[email protected]> wrote:
> >> Hi Alex
>
> >> Sphinx doesn't understand arrays of floats - so you can't have multiple 
> >> lat/lng values for a single record.
>
> >> What you're going to need to do is have a search index for your Site 
> >> model, and search upon that. You can use associations to pull in promotion 
> >> titles and details, as well as adding a promotion id as an attribute.
>
> >> If you want, you could group your Site search results by promotion id, 
> >> thus ensuring you don't get duplicate promotions (if that's what you'd 
> >> prefer).
>
> >> Let me know if you get stuck.
>
> >> Cheers
>
> >> --
> >> Pat
>
> >> On 25/01/2011, at 12:35 AM, Alex wrote:
>
> >>> Hi,
>
> >>> I have models set up as follows:
>
> >>> class Promotion < ActiveRecord::Base
>
> >>>  # associations
> >>>  has_many :campaigns, :dependent => :destroy
> >>>  has_many :sites, :through => :campaigns
>
> >>>  define_index do
> >>>    indexes :title
> >>>    indexes :details
>
> >>>    indexes sites.lat, :as => :site_lat
> >>>    indexes sites.lng, :as => :site_lng
> >>>    has 'RADIANS(sites.lat)', :as => :lat, :type => :float
> >>>    has 'RADIANS(sites.lng)', :as => :lng, :type => :float
> >>>    set_property :latitude_attr   => "lat"
> >>>    set_property :longitude_attr  => "lng"
> >>>    has status, starts_at, expires_at
> >>>  end
> >>> end
>
> >>> class Campaign < ActiveRecord::Base
> >>>  belongs_to :promotion
> >>>  belongs_to :site
> >>> end
>
> >>> class Site < ActiveRecord::Base
> >>>  acts_as_mappable
>
> >>>  # associations
> >>>  has_many :campaigns, :dependent => :destroy
> >>>  has_many :promotions, :through => :campaigns
> >>> end
>
> >>> When I call Promotion.search I only get results for the promotions
> >>> that have a single record in Campaigns. So, If I have a Promotion that
> >>> is associated with more than 1 Site, I don't get any results back for
> >>> that Promotion. For example, I might have a Promotion with a title of
> >>> 'widgets' that is available in Birmingham and London. If I search with
> >>> the keyword 'widget' and location 'London', I would expect my search
> >>> to return the promotion, but, it doesn't. If I delete from the
> >>> Campaigns table the row that associates the promotion to the site in
> >>> Birmingham, and search again, the promotion gets returned
> >>> successfully.
>
> >>> Can anyone advise why this is, and what I need to change about my
> >>> search to be able to return Promotions that are associated with more
> >>> than one site?
>
> >>> Thanks for all your help
> >>> Alex
>
> >>> --
> >>> You received this message because you are subscribed to the Google Groups 
> >>> "Thinking Sphinx" group.
> >>> To post to this group, send email to [email protected].
> >>> To unsubscribe from this group, send email to 
> >>> [email protected].
> >>> For more options, visit this group 
> >>> athttp://groups.google.com/group/thinking-sphinx?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Thinking Sphinx" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/thinking-sphinx?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/thinking-sphinx?hl=en.

Reply via email to