Rails, Sphinx (thinking_sphinx) and sorting from associations
I have 3 models:
Stats (belongs_to :item)
t.integer :skin_i开发者_Python百科d
t.integer :item_id
t.integer :rating
Item (has_many :stats)
and
Skin (has_many :stats)
Using thinking_sphinx i want to create a separate index, for items, sorted by :rating for particular :skin_id
So, i'm trying to:
define_index 'sort_by_rate' do
indexes stats(:rating), :as => :ratings, :sortable => true
end
But this, will generate an index for all :skin_id (in the Stat model), not for particular one.
In other words, i need to gather all items, sorted by Stat.rating, with Stat.skin_id == 1 (for example).
Here is the example of SQL:
"SELECT `stats`.* FROM `stats` INNER JOIN `items` ON `items`.`id` = `stats`.`item_id` WHERE `stats`.`skin_id` = 1 ORDER BY rating DESC"
ANy solutions is very appreciated!
Perhaps you should have in your define_index block:
has :skin_id
and then, when searching, filter by that. Something like this:
Item.search(:with => {:skin_id => skin.id}, :order_by => 'ratings ASC')
精彩评论