Scott Milella
Scott Milella

Reputation: 487

Had to re-write a DB Query for Rails 6 due to Squeel gem: Error: undefined method `call' for #<ActiveRecord::Associations::CollectionProxy []>

I recently had to re-write an APP called CtrlPanel which I have all done and working well. (Thanks to several users on StackOverflow for helping with that!)

I am now onto the second app I have to rewrite called Emissions Gateway. Just like CtrlPanel this WebApp had a gem in it called squeel which uses its own Monkey Patched DSL language for the DB queries so I have to re-write ALL of them in normal Ruby on Rails/ActiveRecord queries.

This APP has a LOT more of these queries and they are more complicated instead just just a few fields mapping to other fields they are using model names and symbol name combinations. I need to re-write all of them and I am having an issue on the very first one.

The error isn't what I was expecting? It is not giving me an error with the query itself it is giving me an error on what is calling the query if I understand it right? This is the error message:

undefined method `call' for #<ActiveRecord::Associations::CollectionProxy []>

According to Better Errors it is the projects_controller.rb that is making the call:

class ProjectsController < ApplicationController
  load_and_authorize_resource
  def dashboard
    @projects = current_user.authorized_projects.order("created_at desc")
  end

What I wanted to make sure of before I start going down a troubleshooting rabbit hole is I wanted to know if my query re-write does look ok, even though it isn't giving me a syntax error I'm sure there can be other things wrong with it and I am still relatively new to this.

Here is the originally query using that darn squeel language:

#Project.joins{vendor}.where{(projects.vendor_id.eq my{ vendor_id }) |
   (vendors.parent_vendor_id.eq my{ vendor_id })}.uniq

Here is my re-written query:

Project.joins(vendor).where({projects.(:vendor_id) => my.(:vendor_id)}) 
        OR
        ({vendors.(:parent_vendor_id) => my(:vendor_id)}).uniq

I thought I would TRY to keep it really simple and only supply the bare minimum. Based on above do I have the syntax of the query correct or am I getting this wrong? I didn't have to re-write any queries on ctrlpanel that used classname.symbole like the queries are in this app. If it helps to know this APP was a much older APP, written in Ruby 2.0 and Rails 3.2. It was a ROYAL PAIN to get it to boot and to get the login prompt to finally come up, I get this error right after it accepts my login.

If I need to post the entire user model, the projects model & controller and the vendor model to be able to answer the question on if my query is right or to reveal more info on this error let me know and I will add them. Was trying to keep it in more simple terms to make it easier if possible, but if that can't be done I will happily supply the rest.

Thanks In advance to anyone who can help.

Scott

Upvotes: 1

Views: 74

Answers (1)

Scott Milella
Scott Milella

Reputation: 487

Thank you to @engineersmnky

This query worked:

Project.joins(:vendor)
       .where(projects: {vendor_id: vendor_id})
       .or(
         Project.joins(:vendor)
                .where(vendors: {parent_vendor_id: vendor_id})
        )

Sorry for the delayed response.

I GREATLY appreciate the help.

Scott

Upvotes: 2

Related Questions