Skip to content

Conflicting Scopes? #520

@bugzbrown

Description

@bugzbrown

I am really not sure what exactly is going on here, and I am pretty sure this is the edge case of the edge case scenarios.

I am implementing trestle in a legacy project here.
I have dozens of models that are pretty much all related.
We are adopting the use of Trestle focused on behavior rather than on "entities". What do I mean by that? We have several "admins" focused on specific actions we want users to take. (Example: I have an action which is "edit the user's address" - this links to customer/address_admin.rb and my form only allows users to alter address details)

OK so everything was going fine... My resources are all working beautifully with initializing them with Trestle.resource(:block_name, [scope: SomeScope], model: SomeModel)
I mean, until I ran into a problem with a specific Model.

Trestle.resource(:date_bulk_update, model: InvoiceGroup) do
   menu do
      item :date_bulk_update,
   end
   # everything else is commented
end

Causes my server to crash. In the logs, the crash seems to be in app/models/invoice_group.rb and the next couple of lines suggest it has something to do with scopes in ActiveRecord

these are my scopes

  scope :find_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  scope :find_by_business_and_bank, lambda { |business, bank|
                                      joins(:businesses)
                                        .where(businesses: { document: business.document })
                                        .where(bank: bank)
                                        .order(created_at: :desc)
                                    }
  scope :monitored, lambda { |bank|
    if bank.slug == 'bank_slug'
      where('invoice_groups.created_at >= ?', 6.months.ago)
    else
      all
    end
  }

Funny/Odd things:

Depending on how I instance InvoiceGroup:

This Crashes:

Trestle.resource(:date_bulk_update, model: InvoiceGroup) do ...

This Works:

Trestle.resource(:invoice_groups) do ...

If I comment or rename my scopes, my server spins up correctly

THIS CRASHES MY SERVER

  # scopes edited to make it easier to read
  scope :find_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  scope :find_by_business_and_bank, lambda { |business, bank| ... }
  scope :monitored, lambda { |bank| ... }

EITHER WORKS

  # scopes edited to make it easier to read
  scope :afind_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  scope :afind_by_business_and_bank, lambda { |business, bank| ... }
  scope :amonitored, lambda { |bank| ... }
  
  ### OR
  
  # scopes edited to make it easier to read
  #scope :find_by_id_or_name, ->(id) { find_by('name = ? or id = ?', id, id.to_i) }
  #scope :find_by_business_and_bank, lambda { |business, bank| ... }
  #scope :monitored, lambda { |bank| ... }

Any idea of what may be going on?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions