ruby on rails - How to filter results with Ransack -
i trying implement ransack search feature on website. tried watching railscasts on ransack , got pretty idea of how implement it. running issue can't seem figure out.
in index action of users controller, have following code:
def index @users = user.same_city_as(current_user) end @users activerecord::relation object. @users capturing users belong same city current_user. in view able iterate through @users , display users belonging same city current_user. good. want able filter these results based on range of age provided user. per railcasts, this:
def index @search = user.search(params[:q]) @users = @search.result end but don't have same city scope anymore. want display users belonging same city current_user default , filter results age.
ransack works scopes, can chain own scopes.
assuming user.same_city_as returns scope (an activerecord::relation instance) can this:
@search = user.same_city_as(current_user).search(params[:q]) @users = @search.result and @search.result returns scope, too, can apply further modifications it, pagination example.
with kaminari like:
@search = user.same_city_as(current_user).search(params[:q]) @users = @search.result.page(params[:page])
Comments
Post a Comment