1

The query below gets me the user's next question based on the status of that question. It gets all the questions for that specific section and then the scope does a LEFT JOIN on the statuses that belong to that user.

My question is, this doesn't seem like a very Railsy way to do it - is there a better way of filtering my table rather than this clumsy AND and .to_s business. My issue is that obviously, if any user has answered that question, then the left join will fill up with that user's answer, whereas I require it to be null.

Essentially the query works but is ugly and I can't figure out if it's the most efficient way!

scope :next_for_user, lambda { |user|
    joins("LEFT JOIN user_question_statuses ON user_question_statuses.question_id = questions.id AND user_question_statuses.user_id = ", user.id.to_s).
    reorder("user_question_statuses.answered ASC NULLS FIRST").
    order("user_question_statuses.updated_at ASC NULLS FIRST").
    limit(1)
  }

Edit:

I realise this method is particularly vulnerable to SQL injection so I've replaced the main line in the query with:

joins(sanitize_sql_array(["LEFT JOIN user_question_statuses ON user_question_statuses.question_id = questions.id AND user_question_statuses.user_id = %d", user.id]))

which seems to work and forces the input to be an integer only.

Edit 2:

My other option is to use the find_each and then user first_or_create to create empty question statuses for that particular section of questions for the current user. This could happen as and when they need them before looking for a question. This would allow me to do a RIGHT JOIN from the questions on to those statuses, knowing they exist but if the first method is efficient and safe (and as Railsy as it can be), then there's not reason to change that.

Edit 3:

I have structured this query in this way because - from the section model that has_many questions - I want to find the next question that should be passed to a user.

To find this I need to join all of the user_question_statuses on to all of the section model's questions. The only way this can be done is on question.id. However, there are many user_question_statuses with that question id for different users. So when joining I need the AND clause to filter down the user_question_statuses to only ones from that user before the join happens. A user hey obviously only have one status per question.

I use a LEFT JOIN so that if a status does not yet exist (they only get created after a user attempts a question for the first time) there are still statuses with NULLs everywhere so that they create a row from which to then move to the top (hence NULLS FIRST) and potentially server to the user.

This may all be extremely unclear!

1
  • @muistooshort - I've updated the answer. Hopefully the explanation makes sense!
    – RichieAHB
    Feb 7, 2015 at 20:17

1 Answer 1

-2

What you did was skipping the ORM layer offered by Active Record and constructed the queries by yourself. Your feeling is correct that this approach as many limitations and is not a well fit to MVC model that rails is following. I would suggest a read through Active Record Query Interface to get concepts of doing it in the rails/oop way

1
  • 1
    I'm familiar with the query interface. However, there is no method for using an AND clause on a JOIN though which require me to go outside of that.
    – RichieAHB
    Feb 7, 2015 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.