How to define indexes for polymorphic relationship?

73 views
Skip to first unread message

puls

unread,
Oct 12, 2009, 9:53:44 AM10/12/09
to Thinking Sphinx
class Credit < ActiveRecord::Base
belongs_to :creditable, :polymorphic => true
belongs_to :user
end

class Bundle < ActiveRecord::Base
belongs_to :user

has_many :items, :dependent => :destroy
has_many :credits, :as => :creditable, :dependent => :destroy
end

class Item < ActiveRecord::Base
belongs_to :user
belongs_to :bundle

has_many :credits, :as => :creditable, :dependent => :destroy
end

class User < ActiveRecord::Base
has_many :items, :dependent => :destroy
has_many :credits, :dependent => :destroy
end

I want to do search on following columns:
Table Column
1) Item name
2) Item keywords
3) Item description
4) Item manufacturer
5) User state
6) User city
7) User zip_code

for user columns I already define indexes in credit model as below:
define_index do
# fields

indexes user.city, :as => :user_city
indexes user.state, :as => :user_state
indexes user.zip_code, :as => :user_zip_code

# attributes
has user_id
set_property :delta => true # for generating indexes automatically
end

So in Credit model how can I define indexes for polymorphic item
columns?

Pat Allan

unread,
Oct 13, 2009, 7:53:46 AM10/13/09
to thinkin...@googlegroups.com
You should be able to reference polymorphic joins normally - provided
there is existing data there so Thinking Sphinx can figure out that
creditable can refer to Item.

indexes creditable(:name), :as => :credit_name
indexes creditable.keywords, :as => :keywords
indexes creditable.description, :as => :description
indexes creditable.manufacturer, :as => :manufacturer

If this doesn't work, let me know what the errors are, and we can work
through it.

Cheers

--
Pat

puls

unread,
Oct 13, 2009, 10:30:19 AM10/13/09
to Thinking Sphinx
Hi Pat,

Thanks for quick reply.

I already solved this problem before I see this post.

Actually there was no data in the credit table so the error was
coming.

Though thanks again for showing interest.

Thanks & Regards,
Pulkit Pancholi

Adam Weiss

unread,
Nov 18, 2009, 8:56:44 PM11/18/09
to Thinking Sphinx
Is there any way to index if there isn't any existing data for a
polymorphic join? If I'd like to deploy an application with no data
but that is set up for indexing.

On Oct 13, 6:53 am, Pat Allan <p...@freelancing-gods.com> wrote:
> You should be able to referencepolymorphicjoins normally - provided  
> there is existing data there so Thinking Sphinx can figure out that  
> creditable can refer to Item.
>
>    indexes creditable(:name),       :as => :credit_name
>    indexes creditable.keywords,     :as => :keywords
>    indexes creditable.description,  :as => :description
>    indexes creditable.manufacturer, :as => :manufacturer
>
> If this doesn't work, let me know what the errors are, and we can work  
> through it.
>
> Cheers
>
> --
> Pat
>
> On 12/10/2009, at 3:53 PM, puls wrote:
>
>
>
> > class Credit < ActiveRecord::Base
> >  belongs_to :creditable, :polymorphic=> true

Pat Allan

unread,
Nov 23, 2009, 12:33:48 AM11/23/09
to thinkin...@googlegroups.com
Hi Adam

Unfortunately there isn't at the moment - but as you do get data, when
you do a full index, you will have the new fields and attributes
picked up through the joins. It's best to run ts:rebuild (instead of
just ts:in) when this is the case, though.

--
Pat
> --
>
> You received this message because you are subscribed to the Google
> Groups "Thinking Sphinx" group.
> To post to this group, send email to thinkin...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=
> .
>
>

Reply all
Reply to author
Forward
0 new messages