Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:58:00+00:00 2026-06-14T12:58:00+00:00

Question class has_many Choices. I need to find a choice by some attribute value

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:58:02+00:00Added an answer on June 14, 2026 at 12:58 pm

    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:

    Enumerable.module_eval do
      def select_by(attribute_name, value)
        detect do |item|
          item.send(attribute_name) == value
        end
      end
    
      def select_by!(attribute_name, value)
        select_by(attribute_name, value) \
          or raise StandardError.new("can't find item with #{attribute_name} equal to #{value} in #{self.inspect}")
      end
    end
    

    Usage would be:

    question.choices.select_by(:id, 7)
    user.question.select_by(:name, "Is it shiny?")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Question domain model designed as follows class Question { List<Choice> choiceCollection;
class Participant has_many :cells end class Round has_many :cells end class Question has_many :cells
I have class Question < ActiveRecord::Base has_many :tasks, :as => :task end class QuestionPlayer
Say you have this: class Question < ActiveRecord::Base has_many :answers end class Answer <
I've got this model class Question < ActiveRecord::Base has_many :votes def to_s hello? end
Given the following associations, I need to reference the Question that a Choice is
I have these model: class Question has_many :answers end class Answer belongs_to :question end
class QuestionGroup < ActiveRecord::Base has_many :questions end class Question < ActiveRecord::Base belongs_to :question_group has_many
Okay, newbie multi-threading question: I have a Singleton class. The class has a Static
A Castle Windsor question: Is it possible to register a class that has an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.