railsadminteam / rails_admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data
MIT License
7.87k stars 2.25k forks source link

Cannot list/sort models with namespaced table names #3620

Open istrasci opened 1 year ago

istrasci commented 1 year ago

Describe the bug Any models that has a namespaced table name of the form self.table_name = 'my_schema.my_table' cannot be listed/sorted. Gives the follow error:

ActiveRecord::UnknownAttributeReference - Query method called with non-attribute argument(s): "my_schema.my_table.my_primary_key asc"

Reproduction steps

  1. Create a model that has a namespaced table name (DBMS-dependent)
  2. Ensure it is being loaded by rails_admin
  3. Click on the model in the left nav to go to the list view.

Expected behavior It should show the list view as normal, and further be able to sort.

Additional context

Workaround

I am able to at least bring up the list view by adding the following model config:

RailsAdmin.config do |config|
  config.model 'MyModel' do
    list do
      sort_by 'my_model.my_primary_key'
    end
  end
end

However, further sorting within the list view fails because it reverts back to trying to sort by using my_schema.my_table.column_to_sort.

Thoughts

I realize this ultimately boils down to an issue with rails (ActiveRecord::QueryMethods#reorder), but it would be nice if rails_admin could check the model's table name, and strip out the schema before listing/sorting.