Commit 186cc14
Fixes #39002 - Handle nil hostgroup in GroupParameter search
When searching for hosts by parameters, the search_by_params method
would crash with "NoMethodError: undefined method 'subtree_ids' for
nil:NilClass" if it encountered a GroupParameter with a nil hostgroup
association (orphaned parameter).
This can occur due to the polymorphic nature of the Parameter model's
reference_id column in the STI table design, where database-level
foreign key constraints are not possible. If a hostgroup is deleted
outside of Rails or cascade delete fails, orphaned GroupParameters
can exist.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit properly handles negated
searches like "not params.foo = bar".
When conditions were blank (due to orphaned parameters) but negate was
present, the code would create a string starting with " NOT(...)" which
caused invalid SQL generation: "NOT COALESCE(, false)".
This commit fixes the issue by ensuring we always build valid SQL:
- If conditions is blank but negate is present: "NOT(negate)"
- If both conditions and negate are present: "conditions AND NOT(negate)"
- If only conditions is present: "conditions" (unchanged)
- If both are blank: "1 = 0" (no results)
This prevents SQL syntax errors while maintaining correct search semantics.
Also added test coverage for negated searches with orphaned parameters.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>1 parent 628206f commit 186cc14
File tree
2 files changed
+65
-3
lines changed- app/models/concerns/hostext
- test/models/concerns/hostext
2 files changed
+65
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
199 | | - | |
200 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
201 | 211 | | |
202 | 212 | | |
203 | 213 | | |
| |||
274 | 284 | | |
275 | 285 | | |
276 | 286 | | |
277 | | - | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
278 | 290 | | |
279 | 291 | | |
280 | 292 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
274 | 324 | | |
275 | 325 | | |
276 | 326 | | |
0 commit comments