Search code examples
ruby-on-railsthinking-sphinx

Thinking Sphinx : index translation attributes


I have a Good model with Globalize3 name tranlation, so: good.name(:en) return en good name. good.name(:ru) return rus good name. I need to sort Good in alphabetical order, depence of current language, so (i think so) i need to make

index ... as :en_name
index ... as :ru_name

And then search (order : "en_name asc" ) for example.

I have:

good.translations
=> [#<Good::Translation id: 5, good_id: 3, locale: "**ru**", name: "**Аурелия** ", description: "\"Lorem ipsum dolor sit amet, consectetur adipisicin...", created_at: "2012-12-24 13:43:37", updated_at: "2012-12-24 13:43:37">,
 #<Good::Translation id: 6, good_id: 3, locale: **"en"**, name: "**Aurelia**", description: "\"Lorem ipsum dolor sit amet, consectetur adipisicin...", created_at: "2012-12-24 13:43:37", updated_at: "2012-12-24 13:43:37">]

And need do index en_name and ru_name to each good. Something like that:

indexes "translations(:name) where locale='en'", :as => :en_name, sortable: :insensitive

But it not working. How to do it?


Solution

  • WHERE clauses shouldn't be a part of field or attribute definitions - as they're within the SELECT clause of the underlying SQL query.

    You can, however, specify your own WHERE conditions easily enough in your index definition:

    indexes translations.name
    # other fields and attributes
    
    where "translations.locale = 'en'"