indexes for chain has_many relationships

7 views
Skip to first unread message

puls

unread,
Nov 6, 2009, 8:52:43 AM11/6/09
to Thinking Sphinx
Hi Pat,

I have following models:

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

class Bundle < ActiveRecord::Base
belongs_to :user

has_many :items, :dependent => :destroy
end

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

has_many :credits, :dependent => :destroy
end

class Credit < ActiveRecord::Base
belongs_to :user
belongs_to :item
belongs_to :bundle

define_index do
# fields
indexes item(:name), :as => :item_name, :sortable => true
indexes item.manufacturer, :as => :item_manufacturer
indexes item.keywords, :as => :item_keywords
indexes item.description, :as => :item_description

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

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

end



Now as you can see in above relationships one user has many bundles
and one bundle has many items.So, I want to define indexing like
"bundle.items(:name)"

I did some fixing but unfortunately it is not working :(

Can you please tell me that does indexing support this type of
relationship? If yes, how?

Thanks & Regards,
Pulkit J. Pancholi

Pat Allan

unread,
Nov 6, 2009, 6:03:04 PM11/6/09
to thinkin...@googlegroups.com
Hi Pulkit

If the index is in Credit, then what you've suggested should work, if
you provide an alias:
indexes bundle.items(:name), :as => :bundle_items

If you want to access the Credit user's bundle items, then this should
work instead:
indexes user.bundles.items(:name), :as => :user_bundle_items

If you're still stuck, let me know.

--
Pat

puls

unread,
Nov 9, 2009, 1:10:45 AM11/9/09
to Thinking Sphinx
Hi Pat,

Thank you for quick and correct response!

The functionality is working fine as you have said.

Thanks & Regards,
Pulkit J. Pancholi

Reply all
Reply to author
Forward
0 new messages