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
> >>>>>>>>> ========
> >>>>>>>>> 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
>
> >>>>>>>>> I ran ts:rebuild, then search this in console:
>
> >>>>>>>>>>> TripDay.search "Cititel Mid Valley"
> >>>>>>>>> => [#<TripDay id: 4, trip_date: "2010-10-06", trip_id: 2,
> >>>>>>>>> created_at:
> >>>>>>>>> "2010-10-21 10:17:18", updated_at: "2010-10-21 10:17:18">]
>
> >>>>>>>>> Few questions:
>
> >>>>>>>>> 1. Previously, was the trip.rb indexing the spots name, city,
> >>>>>>>>> country,
> >>>>>>>>> etc. correctly? They are indexed, am I right?
> >>>>>>>>> 2. With the TripDay now can be indexed, is there anyway for me to
> >>>>>>>>> use
> >>>>>>>>> it as search for my Trips?
>
> >>>>>>>>> Thanks.
>
> >>>>>>>>> On Nov 2, 11:59 am, Pat Allan <[email protected]> wrote:
> >>>>>>>>>> Of course - though I would prefer a donation once the problem's
> >>>>>>>>>> solved ;)http://pledgie.com/campaigns/1752
>
> >>>>>>>>>> --
> >>>>>>>>>> Pat
>
> >>>>>>>>>> On 02/11/2010, at 2:43 PM, Victor wrote:
>
> >>>>>>>>>>> No problem Pat. Can I have a link to make donation? :)
>
> >>>>>>>>>>> On Nov 2, 8:06 am, Pat Allan <[email protected]> wrote:
> >>>>>>>>>>>> Sorry Victor, I'm out of ideas as to what's causing the problem,
> >>>>>>>>>>>> beyond investigating it on the server myself (and even then, I
> >>>>>>>>>>>> couldn't promise anything).
>
> >>>>>>>>>>>> --
> >>>>>>>>>>>> Pat
>
> >>>>>>>>>>>> On 01/11/2010, at 11:33 PM, Victor wrote:
>
> >>>>>>>>>>>>> So when I did the Trip.searc:
>
> >>>>>>>>>>>>>>> Trip.search "Inna Putri Bali"
>
> >>>>>>>>>>>>> => []
>
> >>>>>>>>>>>>> Spots in Trip is not indexed?
>
> >>>>>>>>>>>>> I don't have any sphinx scopes.
>
> >>>>>>>>>>>>> On Nov 1, 8:29 pm, Pat Allan <[email protected]> wrote:
> >>>>>>>>>>>>>> But you're running these commands via console - so we're
> >>>>>>>>>>>>>> avoiding the controllers and views, and it's still not
> >>>>>>>>>>>>>> working...
>
> >>>>>>>>>>>>>> --
> >>>>>>>>>>>>>> Pat
>
> >>>>>>>>>>>>>> On 01/11/2010, at 9:27 PM, Victor wrote:
>
> >>>>>>>>>>>>>>> Could it be that my filter in view or controller wasn't setup
> >>>>>>>>>>>>>>> properly
> >>>>>>>>>>>>>>> to search the right place?
>
> >>>>>>>>>>>>>>> On Nov 1, 6:21 pm, Pat Allan <[email protected]>
> >>>>>>>>>>>>>>> wrote:
> >>>>>>>>>>>>>>>> I live in Melbourne, and I've not been to the Gaol yet ;)
>
> >>>>>>>>>>>>>>>> I'm running out of ideas at the moment... the SQL looks
> >>>>>>>>>>>>>>>> correct, the data's being indexed, and searchd is restarting
> >>>>>>>>>>>>>>>> as expected. You don't have any sphinx scopes (or more
> >>>>>>>>>>>>>>>> importantly - a default sphinx scope) in the Trip model, do
> >>>>>>>>>>>>>>>> you?
>
> >>>>>>>>>>>>>>>> --
> >>>>>>>>>>>>>>>> Pat
>
> >>>>>>>>>>>>>>>> On 01/11/2010, at 9:13 PM, Victor wrote:
>
> >>>>>>>>>>>>>>>>> If I did "Spot.search "Inna Putri Bali"
>
> >>>>>>>>>>>>>>>>>>> Spot.search "Inna Putri Bali"
> >>>>>>>>>>>>>>>>> => [#<Spot id: 2, spot_type: "accommodation", name: "Inna
> >>>>>>>>>>>>>>>>> Putri Bali",
> >>>>>>>>>>>>>>>>> acc_type: "Hotel", operation_hours: "", check_in:
> >>>>>>>>>>>>>>>>> "2000-01-01
> >>>>>>>>>>>>>>>>> 14:00:00", check_out: "2000-01-01 13:00:00", season: "No
> >>>>>>>>>>>>>>>>> season",
> >>>>>>>>>>>>>>>>> prices: "Check their website for latest deals. It's much
> >>>>>>>>>>>>>>>>> che...",
> >>>>>>>>>>>>>>>>> country: "Indonesia", zip: "80363", state: "Bali", city:
> >>>>>>>>>>>>>>>>> "Kuta",
> >>>>>>>>>>>>>>>>> address: "Jalan Kawasan Nusa Dua Resort", contact: "+62
> >>>>>>>>>>>>>>>>> 361771020",
> >>>>>>>>>>>>>>>>> email: "[email protected]", web:
> >>>>>>>>>>>>>>>>> "http://hotelputribali.com",
> >>>>>>>>>>>>>>>>> description:
>
> ...
>
> 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.