Search code examples
ruby-on-railssortingsearchlogic

Searchlogic cannot sort search result


Imagine a code:

search = Project.search(
  :title_or_description_or_child_name_or_child_age_or_inspiration_or_decorating_style_or_favorite_item_or_others_like_any => keys,
  :galleries_id_like_any => @g,
  :styles_id_like_any => @st,
  :tags_like_any => @t
)

search.all returns the rows correctly.

But search.descend_by_views returns nil.

Is this gem buggy? What else should I use then?


Solution

  • I'm not sure why it's not working with search.descend_by_views but this should work:

    search = Post.descend_by_views.search(
      :title => ...
      ...
    )
    

    Instead of search, you could also use scopes for everything else too:

    Post.title_or_anything_like_any(keys).galleries_id_like_any(@g)...descend_by_views.all