Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column_names not accepting proc with hash #382

Closed
Nechitadi opened this issue Nov 9, 2023 · 1 comment
Closed

Column_names not accepting proc with hash #382

Nechitadi opened this issue Nov 9, 2023 · 1 comment

Comments

@Nechitadi
Copy link

Nechitadi commented Nov 9, 2023

The documentation states that: You can specify a scope instead of a where condition string for column_names. We recommend providing a Proc that returns a hash instead of directly providing a hash: If you were to directly provide a scope this would load your schema cache on startup which will break things like rake db:migrate.

This is how I'm trying to define the count

counter_culture :promotion,
    column_name: proc { |l| l.active? ? 'active_customers_count' : nil },
    column_names: -> { {
      Lead.active => :active_customers_count,
      Lead.all => :customers_count
    } }

The error: /Users/Admin/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/counter_culture-1.7.0/lib/counter_culture/extensions.rb:33:in `counter_culture': :column_names must be a Hash of conditions and column names (RuntimeError)

If defining it like this

counter_culture :promotion,
    column_name: proc { |l| l.active? ? 'active_customers_count' : nil },
    column_names: {
      Lead.active => :active_customers_count,
      Lead.all => :customers_count
    }

the above error does not occur anymore, but when trying to fix counts manually using

Lead.counter_culture_fix_counts only: :promotion, where: { promotions: { id: 4162640 } }

the query is not generated properly

irb(main):041:0> Lead.counter_culture_fix_counts only: :promotion, where: { promotions: { id: 4162640 } }
  Lead Load (0.3ms)  SELECT "leads".* FROM "leads" WHERE "leads"."state" = $1  [["state", 2]]
  Promotion Load (1.1ms)  SELECT promotions.id, promotions.id, COUNT(leads.id)*1 AS count, 
promotions.active_customers_count FROM "promotions" LEFT JOIN leads AS leads ON promotions.id = 
leads.promotion_id AND (#<Lead::ActiveRecord_Relation:0x0000000113d7e740>) GROUP BY "promotions"."id" ORDER 
BY "promotions"."id" ASC LIMIT $1  [["LIMIT", 1000]]
/Users/Admin/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/rack-mini-profiler-2.3.1/lib/patches/db/pg.rb:69:in `exec_params': PG::SyntaxError: ERROR:  syntax error at or near ":" (ActiveRecord::StatementInvalid)
LINE 1: ...ds.promotion_id AND (#<Lead::ActiveRecord_Relation:0x0000000...
@magnusvk
Copy link
Owner

magnusvk commented Nov 9, 2023

Looking in the specs, this behavior is covered in the City model and then in the specs here.

At a first glance, your call to counter_culture looks correct, but it's hard to tell because I don't have the full code. Could you take a look at the specs and then open a PR with a failing spec that reproduces the error you're seeing? Otherwise this is unfortunately impossible for me to debug.

@magnusvk magnusvk closed this as completed Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants