Question model has_many Answers. Lines below count how many answers question has and order them according to the amount of answers in DESC order. So, most answered questions first.
@search = Question.find(:all,
joins: :answers,
select: ' "questions".*, count("answers".id) as answers_count',
group: '"questions".id',
order: "answers_count DESC").ransack(params[:q])
@questions = @search.result
I also use ransack gem. For ransack to make search I need to add .ransack(params[:q]) to the @search array and call result method on this array. I though this could work, however it doesn't.
What could I use for ransack to start searching?
You should ransack first, then use it's result to chaining.
ransack_result = Question.ransack(params[:q]).result
@questions = ransack_result.find(:all,
joins: :answers,
select: ' "questions".*, count("answers".id) as answers_count',
group: '"questions".id',
order: "answers_count DESC")