bensheldon/activerecord-has_some_of_many

Stack level too deep with `in_batches`

bensheldon opened this issue · 0 comments

lib/active_record/has_some_of_many/associations.rb:21:in block in build'` seems to recurse

    users_query = User.includes(:recent_newsletter_delivery).confirmed

    query = if Time.current.monday?
              users_query.daily_newsletter_subscribed.or(users_query.weekly_newsletter_subscribed)
            else
              users_query.daily_newsletter_subscribed
            end

    users_to_send_to = []
    query.in_batches(of: BATCH_SIZE) do |users|
  1) SendNewsletterEmails when it has already been sent when subscribed and confirmed daily user sends the email only once
     Failure/Error:
           query.in_batches(of: BATCH_SIZE) do |users|
             users_batch = users.reject do |user|
               recent_newsletter_delivery = user.recent_newsletter_delivery

               already_sent_today = recent_newsletter_delivery && recent_newsletter_delivery.created_at.utc.to_date == Time.now.utc.to_date
               not_yet_today = Time.now.utc.hour < user.newsletter_hour_in_utc

               already_sent_today || not_yet_today
             end


     SystemStackError:
       stack level too deep
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:341:in `new'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:341:in `predicate_builder'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:194:in `join_scope'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency/join_association.rb:45:in `block in join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency/join_association.rb:42:in `reverse_each'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency/join_association.rb:42:in `join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:193:in `make_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `block in walk'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `each'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `flat_map'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `walk'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:98:in `block in join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:96:in `each'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:96:in `flat_map'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:96:in `join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/query_methods.rb:1724:in `build_joins'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/query_methods.rb:1581:in `build_arel'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/query_methods.rb:1490:in `arel'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/activerecord-has_some_of_many-5d34a803deec/lib/active_record/has_some_of_many/associations.rb:34:in `build_scope'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/activerecord-has_some_of_many-5d34a803deec/lib/active_record/has_some_of_many/associations.rb:21:in `block in build'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/builder/association.rb:55:in `instance_exec'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/builder/association.rb:55:in `block in build_scope'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:430:in `instance_exec'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:430:in `scope_for'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:222:in `join_scopes'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/reflection.rb:195:in `join_scope'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency/join_association.rb:45:in `block in join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency/join_association.rb:42:in `reverse_each'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency/join_association.rb:42:in `join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:193:in `make_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `block in walk'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `each'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `flat_map'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:220:in `walk'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:98:in `block in join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:96:in `each'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:96:in `flat_map'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/join_dependency.rb:96:in `join_constraints'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/query_methods.rb:1724:in `build_joins'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/query_methods.rb:1581:in `build_arel'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/relation/query_methods.rb:1490:in `arel'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/activerecord-has_some_of_many-5d34a803deec/lib/active_record/has_some_of_many/associations.rb:34:in `build_scope'
     # /Users/bensheldon/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/activerecord-has_some_of_many-5d34a803deec/lib/active_record/has_some_of_many/associations.rb:21:in `block in build'