Strange result when broadcast_replace_later_to partial with method that is not an attribute

This is perhaps somewhat along the lines of this discussion (another issue on this board) and then again perhaps not - which is why I decided to go with a fresh one.

My issue: part of my partial updates, but one method on the model does not…

I have this fairly simple construct partial with – “the culprit” (resource.nbr_pallets) sitting right in the middle of it –

<!-- views/stock_items/_stock_item.html.erb -->
<tr id="<%= dom_id resource %>"  data-controller="list-item">
  <%= render_component "list_checkbox", resource: resource %>
  <td class="...">
    <%= link_to resource.asset.name, stock_item_path(resource), data: { turbo_frame: "_top" } %>
  </td>
---8<---
  <td class="...">
    <%= resource.nbr_pallets %>
  </td>
  <td class="...">
    <%= resource.quantity %>
  </td>
---8<---
  <td class="...">
    <%= record_state resource.asset %>
  </td>
  </td>
</tr>  

which gets ‘called upon’ when a stock_transaction is processed (and while running its due course the processing updates the stock_item,

# stock_item.rb
  def self.subtract_quantity( s, st, parm)
    sp = st.stocked_product
    sl = st.stock_location
    si = self.find_by( stocked_product_id: sp.id, stock_location: sl.id, batch_number: parm["batchnbr"])
    if si 
      si.update_attribute :quantity, (si.quantity - st.quantity)
    else
      si = self.create_for_stock_transaction( s, sp, sl, parm)
      si.update_attribute :quantity, 0
    end
    si
  end

and after the fact broadcasts like this)

  def broadcast_update 
    if self.asset.deleted_at.nil? 
      broadcast_replace_later_to model_name.plural, partial: self, locals: { resource: self }
      broadcast_replace_later_to "stocked_products", partial: "stocked_products/stocked_product", locals: { resource: self.stocked_product }
    else 
      broadcast_remove_to model_name.plural, target: self
      broadcast_replace_later_to "stocked_products", partial: "stocked_products/stocked_product", locals: { resource: self.stocked_product }
    end
  end

eventually getting ‘merged’ into this view

<!-- views/stocked_products/show.html.erb -->
<div 
  class="..." 
  data-controller="tabs"  
  data-action="speicherMessage@window->tabs#handleMessages"
  data-tabs-active-tab="..." >
  <%= render_component "show_header", 
      title: resource.name, 
      breadcrumbs: @breadcrumbs, 
      current: Current, 
      tabs: [ 
      --8<--
        {id: "stock_items_tab", url: stocked_product_stock_items_url(resource), label: t('.stock_items'), action: "click->tabs#change" },
      --8<--
  </div>

  <div id="stock_items_tab" class="..." data-tabs-target="panel">
    <%= turbo_stream_from "stock_items"  %>
    <%= turbo_frame_tag "stock_items", loading: "lazy", src: stocked_product_stock_items_url(resource) do %>
      <%= t('loading') %>
    <% end %>
  </div>
      --8<--
</div>

and like I started out - most of the partial updates the view just perfectly - but the nbr_pallets function on the stock_item is not executed AFAICS

- and - yes - the partial is working 'cause when I reload the view all is good

BTW: the method on stock_item.rb that is ‘acting up’ looks like this


  def nbr_pallets
    q = stock_transactions.pluck :state
    p = 0
    q.each{ |s| case s when 'RECEIVE'; p+=1 when 'SHIP'; p-=1 end } if q.any?
    p
  end

small update (which I hate to disseminate): it’s bypolar by nature :frowning: meaning that there is not pattern to it - sometimes the nbr_pallets gets updated too!

Latest “error” run produced this snippet in the log:

hours-sidekiq-1  | 2022-05-05T15:31:01.709Z pid=1 tid=umt class=Turbo::Streams::ActionBroadcastJob jid=f2579b1788bbf29e82dbe403 INFO: Performing Turbo::Streams::ActionBroadcastJob (Job ID: 140b4b9d-efcc-4b23-a2f7-e1701327665a) from Sidekiq(default) enqueued at 2022-05-05T15:31:01Z with arguments: "stock_items", {:action=>:replace, :target=>#<GlobalID:0x0000ffffa8a58630 @uri=#<URI::GID gid://app/StockItem/5>>, :targets=>nil, :partial=>#<GlobalID:0x0000ffff708abbf8 @uri=#<URI::GID gid://app/StockItem/5>>, :locals=>{:asset=>#<GlobalID:0x0000ffff708ab248 @uri=#<URI::GID gid://app/Asset/13>>, :resource=>#<GlobalID:0x0000ffff708aaca8 @uri=#<URI::GID gid://app/StockItem/5>>}}