Note that there are some explanatory texts on larger screens.

plurals
  1. POSpree Commerce Image (Asset) Associated with multiple OptionValues
    primarykey
    data
    text
    <p>I'm wondering what the best practice is for setting up a relationship as follows.</p> <p>Currently, Spree offers the following models and associations.</p> <p><strong>asset.rb</strong></p> <pre><code>module Spree class Asset &lt; ActiveRecord::Base belongs_to :viewable, polymorphic: true, touch: true acts_as_list scope: :viewable // .... end end </code></pre> <p><strong>image.rb</strong> (perhaps this is irrelevant, but inherits from asset model)</p> <pre><code>module Spree class Image &lt; Asset // ....... end end </code></pre> <p><strong>variant.rb</strong></p> <pre><code>module Spree class Variant &lt; ActiveRecord::Base has_and_belongs_to_many :option_values, join_table: :spree_option_values_variants, class_name: "Spree::OptionValue" has_many :images, -&gt; { order(:position) }, as: :viewable, dependent: :destroy, class_name: "Spree::Image" // ...... end end </code></pre> <p><strong>option_value.rb</strong></p> <pre><code>module Spree class OptionValue &lt; ActiveRecord::Base belongs_to :option_type, :touch =&gt; true acts_as_list scope: :option_type has_and_belongs_to_many :variants, join_table: 'spree_option_values_variants', class_name: "Spree::Variant" // ...... end end </code></pre> <p>Currently, when an image (asset) is added to a product variant, a single db row is added to the <strong>spree_assets</strong> table. This is desired. Within the spree_assets table is a column called <strong>viewable</strong> which holds the id of the variant. This isn't extremely flexible. Basically, this means that a single image can only be applied to a single variant. Currently the Spree Admin reflects this as well. When uploading a product image, a select box is provided to select a single variant to apply this image to. This could be fine for many stores, but for our store, a <strong>Green T-Shirt</strong> image should apply to Small, Medium, and Large Green T-Shirts.</p> <p><strong>Proposed Solution</strong></p> <p>I have detailed the proposed solution in a <a href="https://docs.google.com/document/d/1q2Xke3BoSxVMKPfVTtvhpVkTa-lpWtdyFfSizCPcg5s/edit?usp=sharing" rel="nofollow">Google Doc</a>.</p> <p>Basically, I'm thinking a new table should be used. This new table would hold foreign keys for the <strong>asset</strong>, <strong>option_value</strong>, and <strong>variant</strong>. This could certainly be completely wrong, but that's why I'm posting here. :)</p> <p>Should I create this new db table? Or, are the relationships currently in place to accommodate my needs?</p> <p>If I do create a new db table, would I need a join model? What should it look like?</p> <p>This may be too abstract for Stack Overflow, and if so, I apologize. Please PM me if you'd simply like me to hire you to carry out the implementation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload