0

I have products that can be a part of an assembly. I only had one assembly, which I called a bundle.

Originally, given that a product had any parts, I included an additional option

If I use

<% if @product.parts.any?(&:in_stock?) %>
 HERE I SHOW OPTION FORM
<% end %>

This works, and displays my option. However now I have another option I'd like to show.

I need to show options to corresponding IDs. It seems that my original product part has an ID of 'bundle', so I tried:

<% if @product.parts.find_by_id('bundle') %>

However my option doesn't load. Any ideas what I could be doing wrong? I don't get an error, it's just not finding that id.

Rails 4 & Spree 2.1+

nil
  • 2,238
  • 1
  • 19
  • 28

2 Answers2

2

Unless you're using a gem like friendly_id, I don't think the id of your parts are going to be 'bundle'. Usually they're an autoincremented integer. Are you 100% sure that that's the id? I would suggest looking at rails built in find methods here. It looks like you might want find_by_name.

kddeisz
  • 5,162
  • 3
  • 21
  • 44
0

My schema revealed that what I was looking for was an integer:

    t.integer "part_id"

That particular product part was loading DB:

Spree::AssembliesPart Load  SELECT "spree_assemblies_parts".* FROM "spree_assemblies_parts" WHERE "spree_assemblies_parts"."assembly_id" = 4 AND "spree_assemblies_parts"."part_id" = 6 LIMIT 1

I then used the part_id that was loaded, which in my case was 6.

This find_by method then worked:

    <% if @product.parts.find_by_id('6') %> 
nil
  • 2,238
  • 1
  • 19
  • 28