The output in script/console is the one below:

Loading development environment (Rails 2.3.8)
** Erubis 2.6.6
>> puts @trip_days.first.sphinx_attributes
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.first
from (irb):1
>>


Below are the associated models code. You can see that I am not using any
other mere options yet, just hard coding it to try out first: .merge(:order
=> 'rating_average DESC')


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
    indexes spots.season, :as => :spot_season
    indexes trip.name, :as => :name, :sortable => true
    indexes trip.duration, :as => :duration
    indexes trip.status, :as => :status
    has spots(:id), :as => :spot_id
    has trip(:rating_average), :as => :rating_average
    has trip_id
  end
end


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
end


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 'relevance'
        order = {:order => '@relevance DESC'}
      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 = {:order => '@relevance DESC'}
      end

      #results = Trip.search params[:keyword], {:conditions => conditions,
:star => true}.merge(order)
      #@trips = results.paginate(:page => params[:page])
      @trip_days = TripDay.published.search params[:keyword], {
          :conditions     => conditions,
          :star           => true,
          :group_by       => 'trip_id',
          :group_function => :attr,
          :page           => params[:page]
        }.merge(:order => 'rating_average DESC')
      @trips = @trip_days.collect { |trip_day| trip_day.trip }
    else
      @trips = Trip.by_status('published').paginate(:all, :page =>
params[:page], :order => 'created_at DESC')
    end
  end
end



Sincerely Yours,
Victor Ng

*Time Zone*: (UTC+08:00) Kuala Lumpur, Malaysia


On Mon, Apr 11, 2011 at 7:50 PM, Pat Allan <[email protected]> wrote:

> There's no difference... if you're getting results, though, then it
> *should* be working.
>
> What's the output if you do the following after the search:
>  puts @trip_days.first.sphinx_attributes
>
> --
> Pat
>
> On 11/04/2011, at 9:42 PM, Victor Ng Khuen Ping wrote:
>
> > Hi Pat,
> >
> > TS 1.3.20
> >
> > There is result, but not sorted. I am suspecting it's not catching any
> value from "rating_average".
> >
> > What' s the difference between trip(:rating_average) and
> trip.rating_average?
> >
> > Thank you.
> >
> > Sincerely Yours,
> > Victor Ng
> >
> > Time Zone: (UTC+08:00) Kuala Lumpur, Malaysia
> >
> >
> > On Mon, Apr 11, 2011 at 7:22 PM, Pat Allan <[email protected]>
> wrote:
> > Hi Victor
> >
> > What version of TS are you using? And does an error happen? No results?
> Or just not sorted as you'd expect?
> >
> > --
> > Pat
> >
> > On 11/04/2011, at 7:51 PM, Victor wrote:
> >
> > > Hi Pat,
> > >
> > > This is my TripDay model index:
> > >
> > >  # 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
> > >    indexes spots.season, :as => :spot_season
> > >    indexes trip.name, :as => :name, :sortable => true
> > >    indexes trip.duration, :as => :duration
> > >    indexes trip.status, :as => :status
> > >    has spots(:id), :as => :spot_id
> > >    #has trip.budget, :as => :budget
> > >    #has trip(:created_at), :as => :created_at
> > >    has trip(:rating_average), :as => :rating_average
> > >    has trip_id
> > >
> > >  #  set_property :delta => true
> > >  end
> > >
> > > But it doesn't work.
> > >
> > > On Apr 11, 10:28 am, Pat Allan <[email protected]> wrote:
> > >> Hi Victor
> > >>
> > >> You'd need to add that column as an attribute in your TripDay index,
> via the trip association.
> > >>
> > >> --
> > >> Pat
> > >>
> > >> On 09/04/2011, at 6:48 PM, Victor wrote:
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>> Hi Pat, using ROR 2.3.8
> > >>
> > >>> I have this in City.rb:
> > >>
> > >>> @trip_days = TripDay.published.search params[:keyword], {
> > >>>          :conditions     => conditions,
> > >>>          :star           => true,
> > >>>          :group_by       => 'trip_id',
> > >>>          :group_function => :attr,
> > >>>          :page           => params[:page]
> > >>>        }.merge(:order => 'rating_average DESC')
> > >>>      @trips = @trip_days.collect { |trip_day| trip_day.trip }
> > >>
> > >>> How can I tell Rails to get the rating_average from Trip model
> column,
> > >>> instead of TripDay model? Because the TripDay model does not have
> > >>> rating_average. It's actually Trip model that gets rated.
> > >>
> > >>> Thank you.
> > >>
> > >>> --
> > >>> 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.
> > >
> >
> > --
> > 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.
>
> --
> 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.

Reply via email to