Comments (7)
Hi,
as you see within the sql output, ActiveRecord auto-assigns the following aliases:
LEFT OUTER JOIN "users" ON "users"."id" = "jobs"."customer_id"
LEFT OUTER JOIN "users" "technicians_jobs" ON "technicians_jobs"."id" = "jobs"."technician_id"
LEFT OUTER JOIN "users" "authors_jobs" ON "authors_jobs"."id" = "jobs"."author_id"
Thus, users => customer, technicians_jobs => technician, authors_jobs => author.
Unfortunately, SearchCop can't rewrite those names (yet). Thus, you have 2 options:
Option a)
Do the join yourself using a scope {} block. The rest can stay as is.
search_scope :search do
attributes :status, :customer_id, :technician_id, :author_id, :ro_number,
:end_behaviour, :ro_value, :created_at
attributes :customer => ['customer.name', 'customer.email', 'customer.phone']
attributes :technician => ['technician.name', 'technician.email', 'technician.phone']
attributes :author => ['author.name', 'author.email', 'author.phone']
scope do
joins("left outer join users customer on customer.id = jobs.customer_id").joins("left outer join users technician on technician.id = jobs.technician_id").joins("left outer join users author on author.id = jobs.author_id")
end
end
Don't forget to additionally preload
customer, technichian and author in your controller to avoid N+1 in your views.
Option b)
Use the aliases auto-assigned by ActiveRecord and tell SearchCop how to map them to your models:
search_scope :search do
attributes :status, :customer_id, :technician_id, :author_id, :ro_number,
:end_behaviour, :ro_value, :created_at
attributes :customer => ['users.name', 'users.email', 'users.phone']
attributes :technician => ['technicians_jobs.name', 'technicians_jobs.email', 'technicians_jobs.phone']
attributes :author => ['authors_jobs.name', 'authors_jobs.email', 'authors_jobs.phone']
aliases :technicians_jobs => :user, :authors_jobs => :user
end
I'd be glad to know if this works for you and which option you'll choose ...
from search_cop.
Thanks for your fast reply, mrkamel! I would love to use option b
. I have tried your code but right now, the produced SQL query looks like this: https://gist.github.com/mbajur/e20ea5d26f8060606621
There are no joins for technicians_jobs
nor authors_jobs
so the query throws an exception. It's probably something trivial but, to be honest, i have no idea what's going on in here so i'm not sure what can i possibly do by myself to fix that. Sorry for this, it's probably caused by late night hours and sharp deadline. However, i WOULD LOVE to use search_cop for allmost all of my future projects cause it's perfect. Just that small glitch...
Edit: I have also tried option a and the generated query is: https://gist.github.com/mbajur/bbd560f4a1713bf68b37 (so it's obviously wrong, the where
part)
from search_cop.
Ok, sry. For option B) you have to alias your associations (technician
and author
) instead of user
. Otherwise SearchCop only eager loads user
. That's the reason for the missing joins in your SQL output.
search_scope :search do
attributes :status, :customer_id, :technician_id, :author_id, :ro_number,
:end_behaviour, :ro_value, :created_at
attributes :customer => ['users.name', 'users.email', 'users.phone']
attributes :technician => ['technicians_jobs.name', 'technicians_jobs.email', 'technicians_jobs.phone']
attributes :author => ['authors_jobs.name', 'authors_jobs.email', 'authors_jobs.phone']
aliases :technicians_jobs => :technician, :authors_jobs => :author
end
Regarding option A), an alias definition is missing. Try this one instead:
search_scope :search do
attributes :status, :customer_id, :technician_id, :author_id, :ro_number,
:end_behaviour, :ro_value, :created_at
attributes :customer => ['customers.name', 'customers.email', 'customers.phone']
attributes :technician => ['technicians.name', 'technicians.email', 'technicians.phone']
attributes :author => ['authors.name', 'authors.email', 'authors.phone']
scope do
joins("left outer join users customers on customers.id = jobs.customer_id").joins("left outer join users technicians on technicians.id = jobs.technician_id").joins("left outer join users authors on authors.id = jobs.author_id")
end
aliases :customers => :customer, :technicians => :technician, :authors => :author
end
from search_cop.
Yes! That's it! Now it's working just fine. Thank you for your time, mrkamel! :)
Just a small thing - for option B - users
in attributes :customer
should be singular
from search_cop.
Yeah, you should probably change it even to customers_jobs
and subsequently add the alias.
Otherwise SearchCop assumes you're referencing the :user
association.
search_scope :search do
attributes :status, :customer_id, :technician_id, :author_id, :ro_number,
:end_behaviour, :ro_value, :created_at
attributes :customer => ['customers_jobs.name', 'customers_jobs.email', 'customers_jobs.phone']
attributes :technician => ['technicians_jobs.name', 'technicians_jobs.email', 'technicians_jobs.phone']
attributes :author => ['authors_jobs.name', 'authors_jobs.email', 'authors_jobs.phone']
aliases :customers_jobs => :customer, :technicians_jobs => :technician, :authors_jobs => :author
end
from search_cop.
However, ActiveRecord will probably use users
as alias name, as long as you don't reference the user
association itself. Thus:
search_scope :search do
attributes :status, :customer_id, :technician_id, :author_id, :ro_number,
:end_behaviour, :ro_value, :created_at
attributes :customer => ['users.name', 'users.email', 'users.phone']
attributes :technician => ['technicians_jobs.name', 'technicians_jobs.email', 'technicians_jobs.phone']
attributes :author => ['authors_jobs.name', 'authors_jobs.email', 'authors_jobs.phone']
aliases :users => :customer, :technicians_jobs => :technician, :authors_jobs => :author
end
from search_cop.
Well, that's the reason i'd prefer option A) for such a case. Consistent and nice naming.
from search_cop.
Related Issues (20)
- globalize with search_cop - unknown attribute HOT 2
- Ignoring MySQL stop words for full-text search HOT 5
- Support for PostgreSQL ENUM HOT 3
- Matching multiple in a has_many HOT 1
- multiple where conditions and concatenated with AND HOT 3
- Attribute (case insensitive / ILIKE) HOT 3
- generator for attribute options HOT 3
- Error when using with non-standard pluralization in class name HOT 2
- DEPRECATION WARNING HOT 2
- How to handle special search logic / virtual attributes HOT 4
- Suggestion: Option for `exact: true` (or `right_wildcard: false`) HOT 1
- Can search_cop work with Postgres hstore? HOT 8
- Suggestion: Add support for sort in the free text search HOT 4
- Searching for empty or non empty with the free text expression HOT 2
- RAILS 6 - Search in ActionText HOT 3
- Search word that include % does not work correctly HOT 2
- Ability to apply Custom Operators or Options to entire project HOT 1
- ActiveRecord::ConfigurationError in v1.2.1 HOT 10
- README error? HOT 6
- Words containing brackets HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from search_cop.