You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But the resulting joined SQL query doesn't include the hostgroups table, so throws an error:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: hostgroups.name: SELECT "reports".* FROM "reports" WHERE "reports"."host_id" IN (SELECT "hosts"."id" FROM "hosts" WHERE (("hostgroups"."name" = 'One')))
I've sort of managed to work around it by calling ScopedSearch::QueryBuilder.build_query directly and then generating an AR query with the right sequence as shown above, but it would be good to have some help building join queries in scoped_search itself - if this is the case, and I'm not missing something blindingly obvious.
In our case the scoped search queries could be querying a variety of objects associated to host depending on user configuration, so hardcoding this isn't an option. As a workaround, it's possible to make an inefficient query with IDs, but this was very slow with large numbers of hosts.
I think this is caused by the Rails 4 changes to the .includes method behaviour. The :includes values generated by the query builder should be :eager_loaded or :joinsed instead.
Diving even deeper into this, looks like the issue may actually be in Rails ignoring .includes(:relation).references(:relation) inside an inner query for a where clause. However, I wonder why a LEFT JOIN is generated in this case instead of an INNER JOIN - since if we are searching on attributes of a related table, it makes no sense including rows which have no relation to it.
In Foreman, we hit an issue in the following scenario:
scoped_search :in => :hostgroup, :on => :name, :complete_value => true, :rename => :hostgroup
We then want to list reports for hosts that match a certain hostgroup, so:
This is equivalent to:
But the resulting joined SQL query doesn't include the hostgroups table, so throws an error:
It seems the right way to do this in Rails is:
(http://guides.rubyonrails.org/active_record_querying.html#eager-loading-multiple-associations)
I've sort of managed to work around it by calling ScopedSearch::QueryBuilder.build_query directly and then generating an AR query with the right sequence as shown above, but it would be good to have some help building join queries in scoped_search itself - if this is the case, and I'm not missing something blindingly obvious.
In our case the scoped search queries could be querying a variety of objects associated to host depending on user configuration, so hardcoding this isn't an option. As a workaround, it's possible to make an inefficient query with IDs, but this was very slow with large numbers of hosts.
Here's a small application and unit test demonstrating it: https://github.com/domcleal/ss_join_search_issue (scoped_search 3.2.0, Rails 4.2.0, sqlite 3.8.8, Ruby 2.0.0).
The text was updated successfully, but these errors were encountered: