Need more information. Basically the code of your view. I don't think you need to write any rails query inside your jquery. This shoud be pretty easy.
So far I assumed, from your form user will receive choices for section to questions. So the steps should be following.
You will bind the change event on your select httm input.
$("#question_section").on(function(){
var value = $(this).value() // get whats the section value/text
})
Now get added an action on your controller where you will count the question with the query. So for this you will need to do to things. Registrar the action in route something like following:
resources :questions do # I assume there is a questions resource
get :count, on: :collection
end
Now this will give you a url as route to that action. Now lets get back to javascript part again.
$("#question_section").on(function(){
var value = $(this).val() // get whats the section value/text
$.ajax({
url: '/questions/count.js', // you can skip the format here
data: {section: value}
success: function(data){
$('input[name="question_count"]').val(data)
}
})
})
So now we are ready with the script. Now lets move to controller.
def count
number_of_questions = Question.select("DISTINCT(section)").where(section: params[:section]).count
render :js => number_of_questions
end
Well this is the steps you may follow. Direct query in the view layer is not acceptable. Responsibility centric design doesn't encourage that. I hope I can give some hints with my approach.