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 8792317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:46+00:00 2026-06-13T22:57:46+00:00

I am trying to find a record based on two associated attributes. The Record

  • 0

I am trying to find a record based on two associated attributes. The Record should be selected, if its association contains those two records.

So far, I tried following – Which seemed to me a very bad practice and I want to avoid using it.

@size = Spree::OptionValue.find(params[:size])
@color = Spree::OptionValue.find(params[:color])

vari = Spree::Variant.all
vari.each do |va|
  if va.option_values.include?(@size && @color)
    @variant = va
  end
end

So far, I also tried

@variant = Spree::Variant.all(:include => :option_values, :conditions => ['option_value.id = ?', params[:color])

This seems to be the way to go, but I can’t seem to figure out the right way to get the result.
The return error I keep on getting is following:

ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry for table “option_values”
LINE 1: …_option_values_variants”.”option_value_id” WHERE (option_val…

EDIT:

I got it working due to the great help given in the accepted answer:

Spree::Variant.joins(:option_values).where("spree_option_values.id in (?)", [size, color])
  • 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-13T22:57:46+00:00Added an answer on June 13, 2026 at 10:57 pm

    First off, your code is probably broken. I doubt that .include?(@size && @color) does what you think it does; you’re effectively only checking if option_values includes @color. This is equivalent to doing (true && @color). If you want to include both values, you need .include?(@size) && .include?(@color).

    So your code should probably look like this:

    vari = Spree::Variant.all
    vari.each do |va|
      if va.option_values.include?(@size) && va.option_values.include?(@color)
        @variant = va
      end
    end
    

    Next, you can make your code much more Ruby-esque:

    @variant = Spree::Variant.all.select do |v|
      v.option_values.include?(@size) && v.option_values.include?(@color)
    end
    

    But it’s far better to actually evaluate the condition at the database level rather than load the entire table into your application. You seem to be looking for all records where the associated OptionValues includes the two you’ve selected into @size and @color.

    The query you’re looking for probably looks something like this:

    Spree::Variant.joins(:option_values).where("option_values.id in (?)", [@size, @color])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find the most recent record based on a date field.
I'm trying to find some records based on the tags they have. In order
I'm trying to find records based on a list of id's. But when the
I'm trying to create a SQL statement to find the matching record based on
I am trying to find records based on the current date. One of the
I'm trying to find the most recent record of a group after doing a
Im trying to find records in a VARCHAR column that may contain a NUL
I am really having trouble trying to figure out how to find certain records
When trying to optimize a query for getting store records based on location, I
I am trying to find records that has the following scenario. ID | name

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.