【发布时间】:2013-02-14 17:41:04
【问题描述】:
我正在使用以下方法在我的应用中国际化模型:
class GenreLocalization < ActiveRecord::Base
attr_accessible :genre_id, :name, :locale
end
class Genre < ActiveRecord::Base
has_many :genre_localizations, :dependent => :destroy
def name(locale = nil)
locale ||= I18n.locale
genre_localizations.find_by_locale(locale).name
end
end
如果我调用genre.name,它将返回当前语言环境中的流派名称。
现在我想根据语言环境用sunspot solr 索引这个模型。我的想法是为每一行存储id、localized_name 和locale 的组合。这样我就可以通过这种方式搜索流派:
search = Genre.search do
keywords params[:search]
with(:locale, 'en')
end
到目前为止,我最好的方法是:
searchable do
Language.supported_locales.each do |locale|
integer :id, :stored => true
text :name, :stored => true, :using => :name(locale)
string :locale
end
end
但是text :name, :stored => true, :using => :name(locale) 行(显然)无效,我一直在努力寻找一种方法来索引本地化名称。
有没有办法做到这一点?这甚至是获得本地化搜索的正确方法吗?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 solr sunspot