Get stuck with OptionType/OptionValue complex with Spree – HELP!

Above is a very simple Spree product with many variants. I just have very simple question:

“If I know the Spree::OptionValue of Red, how could I retrieve all variants of the product that has this Spree::OptionValue?”

I looked around and found function has_option but this function does not limit the variants to a specified product… I am kinda new to Spree and stuck here.. can someone help?

OPTIMAL WAY Thanks to Roman Smirnov for the AR join way, this way is better than Steph’s in performance

product.variants.joins(:option_values).where(["option_values.name = ?", "Red"])
Spree::Variant.joins(:option_values).where(["option_values.name = ?", "Red"])
Product.joins(:variants =>  ption_values).where(["option_values.name = ?", "Red"]).group_by_products_id

UPDATE: Thanks to Stephanie of Endpoint, it’s just few basic AR query:

product.variants.select { |v| v.option_values.include? Spree::OptionValue.find_by_name('Red') }

or, to get all the variants with that option value

Spree::Variant.all.select { |v| v.option_values.include? Spree::OptionValue.find_by_name('Red') }

or, to get all products with variants with that option value

Spree::Variant.all.select { |v| v.option_values.include? Spree::Spree::OptionValue.find_by_name('Red') }.collect { |v| v.product }.uniq

ALTERNATIVE: Thanks to Mel for pointing out that you actually could use Product built-in product scope:

# Get all things that are red.
Spree::Product.with_option('Red')
Advertisements

About Jones Lee

Nothing much about me..

8 responses to “Get stuck with OptionType/OptionValue complex with Spree – HELP!

  1. product.variants.select { |v| v.option_values.include? OptionValue.find_by_name('Red') }

    or, to get all the variants with that option value

    Variant.all.select { |v| v.option_values.include? OptionValue.find_by_name('Red') }

    or, to get all products with variants with that option value

    Variant.all.select { |v| v.option_values.include? OptionValue.find_by_name('Red') }.collect { |v| v.product }.uniq

    You have to do standard ActiveRecord things here. I don’t believe Spree has anything built in to retrieve this data.

  2. Roman Smirnov

    Please, don’t use code examples above if you have more than thousand products.

    Exactly the same results can be reached at database end without a complete enumeration of all variants in Ruby:

    product.variants.joins(:option_values).where(["option_values.name = ?", "Red"])

    Variant.joins(:option_values).where(["option_values.name = ?", "Red"])

    Product.joins(:variants => :option_values).where(["option_values.name = ?", "Red"]).group_by_products_id

  3. stuff = Spree::Product.joins(:variants => :option_values).where([“`spree_option_values`.`presentation` = ?”, “Wet Rated”])

    You’ll want to specify the spree_ prefix in your where bit is all. 🙂 Thanks all for help! 😀

  4. Mel Adamei

    OR! You can use scopes! 🙂

    # Get all things that are Title 24 rated.
    Spree::Product.with_property_value(“Title 24″,”Yes”)

    Or just check the property/option type if you don’t care:

    # Get all things that are red.
    Spree::Product.with_option(“Red”)

  5. @Mel: Thanks for pointing out that scope

    • Mel Adamei

      Any time! 🙂 I’ve had a doozy of time learning all of this Spree stuff…. but I feel like I still barely know anything! haha…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: