Let's say I have People, and People has_many Watermelon .
I have a filter_by that can work for People attributes, for example :
:filter_by => 'has_eaten_today'
开发者_运维问答
But can I do a :filter_by
for the nested attribute of Watermelon? For example :
:filter_by => 'watermelons.created_at'
Thanks!
This question came from me not really understanding Sunspot-solr, but its quite easy.
Just target the nested attributes from your model :
def watermelon_time
self.watermelon.created_at
end
Then add that to
searchable do
time :watermelon_time
..
Then my controller :
query.with(:watermelon_time).equal_to(params[:filter_by] == 'watermelon_time' ? 'true' : false) if params[:filter_by]
Extra Bonus :
Added a link to write this functionality in my :
= link_to 'Watermelon Time', url_for(:overwrite_params => { :filter_by => 'watermelon_time', :page => nil })
Bon Appetite!
精彩评论