Can't select all records then dissociate has many relationship of a collection

Feature(s) impacted

The “Dissociate the relationship” action on a has many collection when I “select all records”

Observed behavior

It fails to dissociate the relationship with a error message “update failed: unexpected error.”
It works fine when I select with “Select current page only” though.

Expected behavior

It dissociates the relationship the same way than when I select records with “Select current page only”

Failure Logs

Looking in the rails server logs, it says

[2023-02-02 10:56:30] Forest 🌳🌳🌳  Association Associate error: undefined method `[]' for nil:NilClass
       raise 'Missing required rendering_id' unless user['rendering_id']
                                                        ^^^^^^^^^^^^^^^^
 /app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/scope_manager.rb:30:in `get_scope_for_user'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/scope_manager.rb:16:in `append_scope_for_user'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/search_query_builder.rb:22:in `perform'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/has_many_getter.rb:68:in `prepare_query'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/has_many_getter.rb:18:in `initialize'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/resources_getter.rb:45:in `new'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/resources_getter.rb:45:in `get_ids_from_request'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/services/forest_liana/has_many_dissociator.rb:20:in `perform'
 	/app/vendor/bundle/ruby/3.1.0/gems/forest_liana-7.7.0/app/controllers/forest_liana/associations_controller.rb:76:in `dissociate'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/abstract_controller/base.rb:215:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal/rendering.rb:53:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/abstract_controller/callbacks.rb:234:in `block in process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/callbacks.rb:118:in `block in run_callbacks'
 	/app/vendor/bundle/ruby/3.1.0/gems/actiontext-7.0.3.1/lib/action_text/rendering.rb:20:in `with_renderer'
 	/app/vendor/bundle/ruby/3.1.0/gems/actiontext-7.0.3.1/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/callbacks.rb:127:in `instance_exec'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/callbacks.rb:127:in `block in run_callbacks'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/callbacks.rb:138:in `run_callbacks'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/abstract_controller/callbacks.rb:233:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal/rescue.rb:22:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal/instrumentation.rb:67:in `block in process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/notifications.rb:206:in `block in instrument'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/notifications.rb:206:in `instrument'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal/instrumentation.rb:66:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal/params_wrapper.rb:259:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/searchkick-4.5.0/lib/searchkick/logging.rb:212:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/abstract_controller/base.rb:151:in `process'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionview-7.0.3.1/lib/action_view/rendering.rb:39:in `process'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal.rb:188:in `dispatch'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_controller/metal.rb:251:in `dispatch'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/routing/route_set.rb:32:in `serve'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/router.rb:50:in `block in serve'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/router.rb:32:in `each'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/router.rb:32:in `serve'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/routing/route_set.rb:852:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/stack.rb:61:in `block in call'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/notifications.rb:206:in `block in instrument'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/notifications.rb:206:in `instrument'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/stack.rb:60:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/engine.rb:530:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/railtie.rb:226:in `public_send'
 	/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/railtie.rb:226:in `method_missing'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/routing/mapper.rb:19:in `block in <class:Constraints>'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/routing/mapper.rb:48:in `serve'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/router.rb:50:in `block in serve'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/router.rb:32:in `each'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/journey/router.rb:32:in `serve'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/routing/route_set.rb:852:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/rack/agent_hooks.rb:31:in `traced_call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/rack/browser_monitoring.rb:34:in `traced_call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:266:in `context'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:260:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/cookies.rb:697:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/warden-1.2.9/lib/warden/manager.rb:36:in `block in call'
 	/app/vendor/bundle/ruby/3.1.0/gems/warden-1.2.9/lib/warden/manager.rb:34:in `catch'
 	/app/vendor/bundle/ruby/3.1.0/gems/warden-1.2.9/lib/warden/manager.rb:34:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/tempfile_reaper.rb:15:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/etag.rb:27:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/conditional_get.rb:40:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/head.rb:12:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/http/permissions_policy.rb:38:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/http/content_security_policy.rb:36:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:266:in `context'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/session/abstract/id.rb:260:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/cookies.rb:697:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.3.1/lib/active_record/migration.rb:603:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/callbacks.rb:99:in `run_callbacks'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/executor.rb:14:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/better_errors-2.9.1/lib/better_errors/middleware.rb:62:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/debug_exceptions.rb:28:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/show_exceptions.rb:26:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/rack/logger.rb:40:in `call_app'
 	/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/rack/logger.rb:25:in `block in call'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/tagged_logging.rb:114:in `block in tagged'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/tagged_logging.rb:38:in `tagged'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/tagged_logging.rb:114:in `tagged'
 	/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/rack/logger.rb:25:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/remote_ip.rb:93:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/request_id.rb:26:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/method_override.rb:24:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/runtime.rb:22:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.3.1/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/server_timing.rb:20:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/executor.rb:14:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/static.rb:23:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.4/lib/rack/sendfile.rb:110:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/actionpack-7.0.3.1/lib/action_dispatch/middleware/host_authorization.rb:137:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/rack-cors-1.1.1/lib/rack/cors.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/railties-7.0.3.1/lib/rails/engine.rb:530:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/newrelic_rpm-8.10.1/lib/new_relic/agent/instrumentation/middleware_tracing.rb:100:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/puma-5.6.5/lib/puma/configuration.rb:252:in `call'
 	/app/vendor/bundle/ruby/3.1.0/gems/puma-5.6.5/lib/puma/request.rb:77:in `block in handle_request'
 	/app/vendor/bundle/ruby/3.1.0/gems/puma-5.6.5/lib/puma/thread_pool.rb:340:in `with_force_shutdown'
 	/app/vendor/bundle/ruby/3.1.0/gems/puma-5.6.5/lib/puma/request.rb:76:in `handle_request'
 	/app/vendor/bundle/ruby/3.1.0/gems/puma-5.6.5/lib/puma/server.rb:443:in `process_client'
 	/app/vendor/bundle/ruby/3.1.0/gems/puma-5.6.5/lib/puma/thread_pool.rb:147:in `block in spawn_thread'
 Cannot load the association reporting on Product 49206.

I think something goes wrong on the forest_liana gem that does not authenticate the forest_user on this specific request?

Context

Rails 7 application
latest forest_liana gem at the time I’m posting this (7.8.0)

Thank you for your time and help :slight_smile:

Hey @Na0na0, and welcome to our community :wave:

I can’t reproduce the issue on my end.
Could you please share your model Product?

Hello @matthv ,

I follow up on this one as the client just send me the model:

== Schema Information

Table name: products

id :integer not null, primary key
name :string
slug :string
created_at :datetime not null
updated_at :datetime not null
category_id :integer
brand_id :integer
citizen_id :integer
archived :boolean
status :integer

class Product < ApplicationRecord
attr_accessor :skip_upsert_v2
include Base
include Duplicable
include ::Products::PartialIndexable
include Search::Model

search_options index_name: “search-products-v2”, engine_name: “engine-products-v2”

searchkick index_name: “products_v1”,
word_middle: [:name, :category_tree_names, :shop_name, :description, :brand_name],
callbacks: false,
locations: [:location]

after_commit → { ::Products::Indexable.reindex(product: self) }, if: :should_reindex?
after_commit → { Product::UpsertV2Job.perform_async(id) }, if: → { Flipper.enabled?(“Bo-dev_9466_duplicate_product_v1_v2”) && !skip_upsert_v2 }
after_save :save_carts, if: :saved_change_to_category_id?

after_touch do
self.skip_upsert_v2 = true
end

belongs_to :category, optional: true

has_and_belongs_to_many :tags, after_add: :reindex_product, after_remove: :reindex_product

has_many :selections, through: :products_selections, source: :selection, after_add: :reindex_selection, after_remove: :reindex_selection
has_many :references, dependent: :destroy
has_many :descriptions, → { where(field: “description”) }, class_name: “I18nproduct”, dependent: :destroy
has_many :sizes, → { distinct }, through: :references
has_many :colors, → { distinct }, through: :references
has_many :measures, → { distinct }, through: :references
has_many :sample_images, through: :samples, source: :images

has_many :products_boosts
has_many :boosts, through: :products_boosts

accepts_nested_attributes_for :fields, :references

validates :name, length: { maximum: 153, message: I18n.t(:too_long) }
validates_associated :fields
validates :name, presence: true, format: { with: REGEX_WITH_FIRST_ALPHANUMERIC, message: I18n.t(:allowed_characters) }
validates :shop, presence: true

scope :order_by, ->(type, suffix) { select(“products.*, MIN(pr_references.base_price)”).joins(:references).saleable.group(“products.id”).reorder(type + " " + suffix) }
scope :most_expensive, → { joins(:references).maximum(“pr_references.base_price”) }
scope :saleable, → { joins(:shop, :references).where({ archived: false, status: 1 }).where(shops: { deleted_at: nil, is_template: false }).where.not(pr_references: { base_price: [nil, 0, 0.0], quantity: [nil, 0] }).distinct }
scope :with_delivery_services, ->(delivery_services = ) { where(“delivery_services @>ARRAY[?]”, delivery_services) }

def reindex_product(tag)
::Products::Indexable.reindex(product: self, scope: :tags_only) if should_reindex?
end

def reindex_selection(selection)
::Products::Selections::Indexable.reindex(product: self, selection: selection) if should_reindex?
end

def ordered_colors
colors.order(:name)
end

def set_kind(value: “simple”)
value = “simple” if references.count { |element| !element.marked_for_destruction? } == 1
value = “variable” if references.count { |element| !element.marked_for_destruction? } > 1
value = “service” if is_a_service
self.kind = value
end

def search_data
full_index
end

def description
descriptions.find_by(lang: I18n.locale)&.content || descriptions.find_by(lang: I18n.default_locale)&.content
end

def url
category_tree_slug = category&.slug
city = shop&.city
“/fr/#{city&.slug}/#{shop&.slug}/#{category_tree_slug}/produits/#{slug}”
end

def saleable?
references.saleables.any?
end

def has_an_image?
images.any?
end

def has_an_image_or_sample_image?
sample_images.any? || images.any?
end

def add_error_messages_to_put_product_online
errors.add(:base, I18n.t(:you_have_to_add_a_category_to_put_this_product_online)) unless category || references.any?
unless references.any? && references.saleables.any?
errors.add(:base, I18n.t(:you_have_to_fill_the_price_and_the_quantity_of_one_reference_to_put_this_product_online))
end
errors.add(:base, I18n.t(:you_have_to_add_at_least_one_service_of_one_reference_to_put_this_product_online)) if references.any?
errors.add(:base, I18n.t(:product_unclassified_category, product_id: id)) if ::Products::CategoriesSpecifications::IsUnclassified.new.is_satisfied_by?(category)
end

def add_error_messages_to_update_product_status
errors.add(:base, I18n.t(:product_price_error)) if first_reference_price <= 0.0
end

def change_status
if online?
offline!
elsif offline?
if ::Products::StatusSpecifications::CanBeOnline.new.is_satisfied_by?(self)
online!
elsif ::Products::StatusSpecifications::CanBeOffline.new.is_satisfied_by?(self)
offline!
add_error_messages_to_put_product_online
elsif !::Products::StatusSpecifications::CanBeUpdated.new.is_satisfied_by?(self)
add_error_messages_to_update_product_status
end
end
end

def set_property property
delete_similar_properties property
properties << property
save
end

def first_sample
return nil unless samples.any?
return samples.first unless references.saleables.any?
return samples.default_sample.first if samples.default_sample.first && samples.default_sample.first.references.saleables.any?
references.saleables.first.sample
end

def set_global_good_deal params
good_deal = GoodDeal.create(params)
product = references.first.product
Products::SetMassivePromotion.new(good_deal, product.shop, [product]).call
end

def delete_similar_properties similar_property
properties.where(characteristic: similar_property.characteristic).delete_all
end

def cheapest_ref
references.where.not(base_price: [nil, 0], quantity: [nil, 0]).order(“pr_references.base_price ASC”).first if references.any?
# references.order(“pr_references.base_price ASC”).saleables.first if references.any?
end

def unique_reference
(references.count == 1) ? references.first : nil
end

def has_promotion?
references.joins(:good_deal).saleables.where([“good_deals.discount > ? AND (? BETWEEN good_deals.starts_at AND good_deals.ends_at)”, 0, Time.zone.now]).any?
end

def self.get_products_created_during_month(date)
where(“products.created_at BETWEEN ? AND ?”, date.beginning_of_month, date.end_of_month).online.count
end

def self.get_products_shared_by_cityzens_during_month(date)
joins(:citizens).where(“products.created_at BETWEEN ? AND ?”, date.beginning_of_month, date.end_of_month).submitted.count
end

def self.get_total_products_created_before_month(date)
(date > Time.zone.now) ? 0 : where(“products.created_at <= ?”, date.end_of_month).online.count
end

def self.get_total_products_deleted_before_month(date)
(date > Time.zone.now) ? 0 : where(“products.created_at <= ? AND archived = ?”, date.end_of_month, true).count
end

def first_reference_price
references&.first&.price
end

def last_reference_price
references&.last&.price
end

def run_upsert_v2_job
UpsertV2Job.perform_async(id)
end

def references_with_size
references.where.not(size: nil)
end

def self.indice_mappings
{
mappings: {
properties: {
insee_code: {
type: “text”,
fields: {
keyword: {
type: “keyword”,
ignore_above: 256
}
}
},
department_number: {
type: “text”,
fields: {
keyword: {
type: “keyword”,
ignore_above: 256
}
}
},
location: { type: “geo_point” }
}
}
}
end

def self.result_mappings
{
id: “Integer”,
is_a_service: “Boolean”,
is_newly_created: “Boolean”,
has_images: “Boolean”,
on_discount: “Boolean”,
on_sale: “Boolean”,
in_stock: “Boolean”,
shop_is_template: “Boolean”,
in_holidays: “Boolean”,
discount_value: “Float”,
base_price: “Float”,
price: “Float”,
location: “Geolocation”
}
end

private

def save_carts
Products::SaveActiveCartsJob.perform_async(self.id)
end
end

Thanks!

Hello @lionel.bouzonville,

The issue has been identified and fixed in version 8.0.5.
@Na0na0 can you try to switch to forest >= 8.0.5 ?

Tell me if it works :slight_smile: