0

I am pretty new in rails and honestly, I am struggling with queries even after multiple researchs. Here is my simple schema:

enter image description here

So basically, a question has many options, an option belongs to a question and has many answer, and an answer belongs to an option and has many users.

I don't think it s necessary to post the models code since it is just like i mentioned above.

What i would like to do is given a question option, see if a particular user already checked it (so look in the answer table if there is a row matching a given id_option, user_id and user_type). So in my haml loop, when displaying the different question option, i'm calling a method of my question_option model just like this :

- question.question_option.all.each do |option| #{option.title} .check - if option.selected_by(current_actor) = check_box_tag(option.id, "checked",true, class: 'styled-checkbox') - else = check_box_tag(option.id, "checked",false, class: 'styled-checkbox') 

and the method called :

def selected_by(answerer) answer_match = ::Vacancies::QuestionOption .joins(:answers) .where(answerer_id: answerer.id, answerer_type:answerer.type ) response = answer_match.find(self.id) return response end 

This method is located in my QuestionOption model and leads to no errors but it s not working ever. Can you help me transform this query to make it work with ActiveRecord ? Thanks

3
  • "I don't think it s necessary to post the models code since it is just like I mentioned above.", please do it. Personally, it's easier to see the models and know how to replicate your scenario. Commented Mar 30, 2021 at 9:44
  • You are right i should have done it but since i resolved the problem i think this is not necessary anymore. Anyway, thanks for your interest to it. :D Commented Mar 30, 2021 at 10:09
  • You should post your solution below if you resolved it yourself. Commented Mar 30, 2021 at 11:02

1 Answer 1

1

Try the below code. It checks if there are any answers by the user passed in the params on the question. I think this is what you intended to do as well-

def selected_by(answerer) answers = Answer.where(answerer_id: answerer.id, answerer_type:answerer.type, id_option: self.id) answers.exists? end 
Sign up to request clarification or add additional context in comments.

2 Comments

Might be more performant to use exists? at the end of the query instead of saving to variable and calling present?. also could just use .where(answerer: answerer), Rails magic will compare both the type and id there. And then one final nag, since your expecting a boolean, it's convention to use a predicate method, ie def selected_by?(answerer)
You passed from loading a single database record to query all of them and do nothing with them, as stated by @Int'lManOfCodingMystery, the best improvement is to use exists? if you don't need to access any records' columns.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.