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

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -