RailsAdmin filters don't work if there are multiple connections on the same model

When one class has more than one association belongs_to

with another class, RailsAdmin generates an invalid request when the user tries to filter the second association. Practical example:

class Issue < ActiveRecord::Base
  belongs_to(:author, class_name: 'Worker', foreign_key: 'author_id', inverse_of: :authored_issues)
  belongs_to(:assignee, class_name: 'Worker', foreign_key: 'assignee_id', inverse_of: :assigned_issues)
end

class Worker < ActiveRecord::Base
  has_many(:authored_issues, class_name: 'Issue', foreign_key: 'author_id', inverse_of: :author)
  has_many(:assigned_issues, class_name: 'Issue', foreign_key: 'assignee_id', inverse_of: :assignee)
end

      

When you access /admin/issues

and try to filter by destination, the generated SQL produces incorrect results as it doesn't know that the table was an alias and filters by the author instead:

SELECT  "issues"."id" AS t0_r0, "issues"."title" AS t0_r1, "issues"."description" AS t0_r2, "issues"."author_id" AS t0_r3, "issues"."assignee_id" AS t0_r4, "issues"."created_at" AS t0_r5, "issues"."updated_at" AS t0_r6, "users"."id" AS t1_r0, "users"."name" AS t1_r1, "users"."created_at" AS t1_r2, "users"."updated_at" AS t1_r3, "assignees_issues"."id" AS t2_r0, "assignees_issues"."name" AS t2_r1, "assignees_issues"."created_at" AS t2_r2, "assignees_issues"."updated_at" AS t2_r3
FROM "issues"
LEFT OUTER JOIN "users" ON "users"."id" = "issues"."author_id"
LEFT OUTER JOIN "users" "assignees_issues" ON "assignees_issues"."id" = "issues"."assignee_id"
WHERE ((LOWER(users.name) LIKE '%asdf%'))  ORDER BY issues.id desc LIMIT 20 OFFSET 0

      

How to fix it? sentence WHERE

must use "assignees_issues".name

. Is this a bug in ActiveRecord, RailsAdmin or my application?

+3


source to share


1 answer


I had to use the RailsAdmin DSL to specify the new name for the aliases. Basically:



 config.model 'Issue' do
    list do
      field :title
      field :description
      field :author
      field :assignee do
        searchable do
          {'assignees_issues' => 'name'}
        end
      end
    end
  end

      

+3


source







All Articles