Skip to content

Bug: Deep association search (two levels) not working in Ransack 4.3.0 #1558

@souravforsycamore

Description

@souravforsycamore

Ransack version: 4.3.0
Rails version: 7.2.2.1
Ruby version: 3.3.7

🧩 Problem
Searching through a two-level nested association is not working, even with ransackable_associations and ransackable_attributes explicitly defined as per the Ransack 4.3.0 documentation.

✅ Expected Behavior
Given the following model structure:

class Comment < ApplicationRecord
  belongs_to :post
end

class Post < ApplicationRecord
  belongs_to :user
end

class User < ApplicationRecord
  # has attribute :email
end

I expect this query to work:

Comment.ransack(post_user_email_cont: "[email protected]").result
...and generate SQL similar to:

SELECT "comments".* FROM "comments"
INNER JOIN "posts" ON "posts"."id" = "comments"."post_id"
INNER JOIN "users" ON "users"."id" = "posts"."user_id"
WHERE "users"."email" ILIKE '%[email protected]%'

❌ Actual Behavior
The search query returns no results, or an error, even when:

All associations are valid

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions