Hi Victor I think duration and name should be fields (given that's what they were originally) - so use 'indexes' instead of 'has'. This will ensure they're actually treated as string data for search queries.
-- Pat On 08/11/2010, at 5:12 PM, Victor wrote: > Hey Pat, > > I'm suspecting TripDay couldn't index using this: > > has trip.budget, :as => :budget > has trip.created_at, :as => :created_at > has trip.rating_average, :as => :rating_average > > The duration that works mentioned above, could be directly from > indexes of Trip instead. > > On Nov 6, 11:25 pm, Victor <[email protected]> wrote: >> Almost there, Pat, almost there! >> >> trip_day.rb >> =========== >> >> class TripDay < ActiveRecord::Base >> belongs_to :trip >> >> has_many :trip_day_spots >> has_many :spots, :through => :trip_day_spots >> >> # ThinkingSphinx Index >> define_index do >> indexes spots.name, :as => :spot_name >> indexes spots.city, :as => :spot_city >> indexes spots.state, :as => :spot_state >> indexes spots.country, :as => :spot_country >> has trip_id >> has trip.name, :as => :name >> has trip.duration, :as => :duration >> has trip.budget, :as => :budget >> has trip.created_at, :as => :created_at >> has trip.rating_average, :as => :rating_average >> end >> end >> >> trips_controller.rb >> ============ >> >> class TripsController < ApplicationController >> before_filter :require_user, :except => [:show, :index] >> >> def index >> if params[:filter] == 'on' >> country = params[:country] == 'All' ? {} : {:spot_country => >> params[:country]} >> duration = params[:days].blank? ? "" : params[:days] >> >> if !duration.blank? && country >> conditions = {:duration => duration}.merge(country) >> elsif !duration.blank? && country.blank? >> conditions = {:duration => duration} >> else country && duration.blank? >> conditions = country >> end >> >> keyword = params[:keyword].blank? ? "" : params[:keyword] >> >> case params[:order] >> when 'date_descend' >> order = {:order => 'created_at DESC'} >> when 'date_ascend' >> order = {:order => 'created_at ASC'} >> when 'rating_descend' >> order = {:order => 'rating_average DESC'} >> when 'rating_ascend' >> order = {:order => 'rating_average ASC'} >> when 'budget_dscend' >> order = {:order => 'budget DESC'} >> when 'budget_ascend' >> order = {:order => 'budget ASC'} >> else >> order = {} >> end >> >> #results = Trip.search params[:keyword], {:conditions => >> conditions, :star => true}.merge(order) >> #...@trips = results.paginate(:page => params[:page]) >> @trip_days = TripDay.search(params[:keyword], { >> :conditions => conditions, >> :star => true, >> :group_by => 'trip_id', >> :group_function => :attr, >> :page => params[:page] >> }.merge(order)) >> @trips = @trip_days.collect { |trip_day| trip_day.trip } >> else >> @trips = Trip.paginate(:all, :page => params[:page], :order => >> 'created_at DESC') >> end >> end >> end >> >> Trip's index.html.erb, I have changed: >> >> From >> <%= pluralize @trips.total_entries, 'trip' %></span> >> To >> <%= pluralize @trip_days.total_entries, 'trip' %></span> >> >> From >> <%= will_paginate @trips %> >> To >> <%= will_paginate @trip_days %> >> >> Other attributes in Trip's index.html.erb refers directly to Trip, >> with this tag: <% @trips.each do |trip| %> which works fine. >> >> Other filter such as trip.duration, spot.address, spot.name all worked >> fine. >> >> Now one last problem here: >> I can't filter by the "trip.name", and can't sort by "rating_average" >> and "created_at" even though I have put them in "trip_day.rb". >> >> On Nov 6, 7:21 am, Pat Allan <[email protected]> wrote: >> >>> Ah, I removed the pagination with the collect... of course. >> >>> Probably best to keep the trip_day objects separate, and use that for >>> pagination: >> >>> @trip_days = TripDay.search(params[:keyword], { >>> :conditions => conditions, >>> :star => true, >>> :group_by => 'trip_id', >>> :group_function => :attr, >>> :page => params[:page] >>> }.merge(order)) >>> @trips = @trip_days.collect { |trip_day| trip_day.trip } >> >>> And then, you can use @trips for looping through results, and @trip_days >>> for pagination information (ie: total_entries). >> >>> However, this doesn't get around the attribute issue. You could pull the >>> attributes all into the TripDay index: >> >>> has trip.budget, :as => :budget >>> has trip.created_at, :as => :created_at >>> has trip.rating_average, :as => :rating_average >> >>> This is a pretty massive workaround just because the Trip index isn't >>> working as we'd like, though... >> >>> -- >>> Pat >> >>> On 05/11/2010, at 5:21 PM, Victor wrote: >> >>>> Argh, it was in the search function you added: >> >>>> @trips = TripDay.search(params[:keyword], { >>>> :conditions => conditions, >>>> :star => true, >>>> :group_by => 'trip_id', >>>> :group_function => :attr, >>>> :page => params[:page] >>>> }.merge(options)).collect { |trip_day| trip_day.trip } >> >>>> I changed the last line .merge(options) to .merge(order) and I "think" >>>> it is going there now! Great. >> >>>> Now when the app tries to render the index.html.erb to show the search >>>> results, it gives me this: >> >>>> ActionView::TemplateError (undefined method `total_entries' for >>>> []:Array) on line #11 of app/views/trips/index.html.erb: >>>> 8: <% if params[:filter] == 'on' %> >>>> 9: <h1> >>>> 10: Showing <span id="total-results" class="filtered"> >>>> 11: <%= pluralize @trips.total_entries, >>>> 'trip' %></span> >>>> 12: <% if !params[:keyword].blank? %> >>>> 13: on <span id="keywords" class="filtered"><%= >>>> params[:keyword] %></span> >>>> 14: <% end %> >> >>>> I think this is probably because it is searching in TripDay and there >>>> are no attributes found in TripDay. >> >>>> What should I do to display the attributes from Trip instead of >>>> TripDay? Because this happens to my wildcard search too. >> >>>> On Nov 5, 2:05 pm, Pat Allan <[email protected]> wrote: >>>>> What about in your controller? >> >>>>> -- >>>>> Pat >> >>>>> On 05/11/2010, at 5:01 PM, Victor wrote: >> >>>>>> When I performed a search, the following 500 occured: >> >>>>>> Processing TripsController#index (for 115.164.60.198 at 2010-11-05 >>>>>> 13:50:19) [GET] >>>>>> Parameters: {"country"=>"All", "days"=>"", "action"=>"index", >>>>>> "filter"=>"on", "order"=>"date_descend", "controller"=>"trips", >>>>>> "keyword"=>"mid valley"} >> >>>>>> NameError (undefined local variable or method `options' for >>>>>> #<TripsController:0xb693d44>): >> >>>>>> I did not have any variable "options" anywhere in my trip.rb, or >>>>>> Trip's index.html.erb. >> >>>>>> On Nov 4, 9:37 pm, Victor <[email protected]> wrote: >>>>>>> Sorry Pat, we posted at the same time. Will get back to you when I >>>>>>> have tried that. Thanks. >> >>>>>>> On Nov 4, 9:29 pm, Victor <[email protected]> wrote: >> >>>>>>>> Hi Pat, >> >>>>>>>> I am sorry that I may need step-by-step guide here. >> >>>>>>>> Ok, I have added "has trip_id" in the TripDay model. >> >>>>>>>> Next is the trips_controller.rb. If you see my code above, I have also >>>>>>>> added ":group_by => trip_id" and ":group_function => :attr" to the >>>>>>>> Trip.search. >> >>>>>>>> The filter form is in my Trips' index.html.erb. I notice that I >>>>>>>> haven't really included the Trip.search to search TripDay as well >>>>>>>> because when I search, it still returns zero result. How should I add >>>>>>>> that? >> >>>>>>>> Thanks for your help. Really appreciate it. >> >>>>>>>> On Nov 4, 9:17 pm, Pat Allan <[email protected]> wrote: >> >>>>>>>>> No worries about being a noob - we've all got to start somewhere :) >> >>>>>>>>> Yes, you need to add 'has trip_id' to the define_index block in your >>>>>>>>> TripDay model - if the attribute doesn't exist, you won't be able to >>>>>>>>> group by it. >> >>>>>>>>> Cheers >> >>>>>>>>> -- >>>>>>>>> Pat >> >>>>>>>>> On 03/11/2010, at 7:11 PM, Victor wrote: >> >>>>>>>>>> Hi Pat >> >>>>>>>>>> Here are the models so far: >> >>>>>>>>>> trip_day.rb >>>>>>>>>> ========= >>>>>>>>>> class TripDay < ActiveRecord::Base >>>>>>>>>> belongs_to :trip >> >>>>>>>>>> has_many :trip_day_spots >>>>>>>>>> has_many :spots, :through => :trip_day_spots >> >>>>>>>>>> # ThinkingSphinx Index >>>>>>>>>> define_index do >>>>>>>>>> indexes spots.name, :as => :spot_name >>>>>>>>>> indexes spots.city, :as => :spot_city >>>>>>>>>> indexes spots.state, :as => :spot_state >>>>>>>>>> indexes spots.country, :as => :spot_country >>>>>>>>>> end >>>>>>>>>> end >> >>>>>>>>>> Trip.rb >>>>>>>>>> ======= >>>>>>>>>> class Trip < ActiveRecord::Base >>>>>>>>>> belongs_to :user, :counter_cache => true >> >>>>>>>>>> has_many :spot_trips, :dependent => :destroy >>>>>>>>>> has_many :spots, :through => :spot_trips >> >>>>>>>>>> has_many :trip_days, :dependent => :destroy >>>>>>>>>> has_many :trip_reviews, :dependent => :destroy >> >>>>>>>>>> # ThinkingSphinx Index >>>>>>>>>> define_index do >>>>>>>>>> indexes :name, :sortable => true >>>>>>>>>> indexes duration >>>>>>>>>> has budget, created_at, rating_average >>>>>>>>>> end >>>>>>>>>> end >> >>>>>>>>>> trip_day already belongs to Trip. Must I put in "has trip_id" in >>>>>>>>>> trip_day.rb? >> >>>>>>>>>> Sorry for being a noob here. >> >>>>>>>>>> Thanks. >> >>>>>>>>>> On Nov 3, 2:43 pm, Pat Allan <[email protected]> wrote: >>>>>>>>>>> Hi Victor >> >>>>>>>>>>> Well, it's great that it works here. >> >>>>>>>>>>> 1. You are indeed right, you're indexing columns from the spots >>>>>>>>>>> association as fields. >>>>>>>>>>> 2. You could add an attribute of trip_id to TripDay's index: >>>>>>>>>>> has trip_id >>>>>>>>>>> And then group the results by >>>>>>>>>>> trip_id...http://freelancing-god.github.com/ts/en/searching.html#grouping >> >>>>>>>>>>> That said, it *should* work for Trip. Perhaps try stopping Sphinx, >>>>>>>>>>> deleting the index files, and then run rake ts:rebuild to generate >>>>>>>>>>> it all from scratch for sure? >> >>>>>>>>>>> -- >>>>>>>>>>> Pat >> >>>>>>>>>>> On 03/11/2010, at 4:55 PM, Victor wrote: >> >>>>>>>>>>>> Hi Pat, >> >>>>>>>>>>>> I have added indexes to the following >> >>>>>>>>>>>> trip_day.rb >> >> ... >> >> read more ยป > > -- > 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. > -- 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.
