ruby on rails - to make .ransack(params[:q]) operate on the array -
question model has_many answers. lines below count how many answers question has , order them according amount of answers in desc order. so, answered questions first.
@search = question.find(:all, joins: :answers, select: ' "questions".*, count("answers".id) answers_count', group: '"questions".id', order: "answers_count desc").ransack(params[:q]) @questions = @search.result
i use ransack gem. ransack make search need add .ransack(params[:q]) @search array , call result method on array. though work, doesn't.
what use ransack start searching?
you should ransack first, use it's result chaining.
ransack_result = question.ransack(params[:q]).result @questions = ransack_result.find(:all, joins: :answers, select: ' "questions".*, count("answers".id) answers_count', group: '"questions".id', order: "answers_count desc")
Comments
Post a Comment