Question class has_many Choices. I need to find a choice by some attribute value (usually id).
To clarify: parent record (question) is passed around different objects and methods and they search for children using different attributes and values.
I could do question.choices.find(choice_id) but this will generate a SQL query every time.
Knowing questions usually have only a handful choices and that there will be many different searches on one parent record, I suspect it will be cheaper to load whole association and search in Ruby:
question.choices.detect{|choice| choice.id == some_choice_id}
Detect comes from Enumerable and forces Rails to load the association on first call (and use cached data on future searches). This also plays nicely with eager loading: I can do Question.all(include: choices) and will whole tree loaded with two queries and then searching is very fast. It doesn’t read very well though.
It gets even more hairy if I want to simulate find! behaviour:
question.choices.detect{|choice| choice.id == choice_id} or raise ActiveRecord::RecordNotFound.new
This does exactly what I want, I’m just looking for better syntax.
Does Rails provide some sort of dynamic finders that behave this way (force loading of whole association on first query and then search this preloaded set)?
I suspect I’m missing something baked in Rails or available in popular gem. If this is not the case, I’ll just roll my own helpers:
Usage would be: