ransack icon indicating copy to clipboard operation
ransack copied to clipboard

Arel::Table removes alias 'table_name' for 'name'

Open nlsrchtr opened this issue 1 year ago • 5 comments

As with this commit Arel removes the alias 'table_name', which is currently actively used in here.

I think there might be the need for shipping an update to support the latest Rails master.

nlsrchtr avatar May 05 '23 09:05 nlsrchtr

As requested in the Contributing Guide, I would like to extend this issue report, by adding reproducable steps:

  • git clone https://github.com/activerecord-hackery/ransack.git
  • cd ransack
  • RAILS=main bundle install
  • bundle exec rake spec

This leads to 101 failure, I'll only quote the last one:

  101) Ransack::Search#method_missing allows chaining to access nested conditions
       Failure/Error: table        = attr.arel_attribute.relation.table_name
       
       NoMethodError:
         undefined method `table_name' for #<Arel::Table:0x000000010abfa878 @name="people", @klass=Person(id: integer, parent_id: integer, name: string, email: string, only_search: string, only_sort: string, only_admin: string, new_start: string, stop_end: string, salary: integer, life_start: date, awesome: boolean, terms_and_conditions: boolean, true_or_false: boolean, created_at: datetime, updated_at: datetime), @type_caster=#<ActiveRecord::TypeCaster::Map:0x000000010a973578 @klass=Person(id: integer, parent_id: integer, name: string, email: string, only_search: string, only_sort: string, only_admin: string, new_start: string, stop_end: string, salary: integer, life_start: date, awesome: boolean, terms_and_conditions: boolean, true_or_false: boolean, created_at: datetime, updated_at: datetime)>, @table_alias=nil>
       # ./lib/ransack/adapters/active_record/context.rb:16:in `type_for'
       # ./lib/ransack/nodes/attribute.rb:35:in `type'
       # ./lib/ransack/nodes/condition.rb:266:in `default_type'
       # ./lib/ransack/nodes/condition.rb:25:in `extract'
       # ./lib/ransack/nodes/grouping.rb:175:in `write_attribute'
       # ./lib/ransack/nodes/grouping.rb:155:in `block in build'
       # ./lib/ransack/nodes/grouping.rb:150:in `each'
       # ./lib/ransack/nodes/grouping.rb:150:in `build'
       # ./lib/ransack/nodes/grouping.rb:146:in `new_grouping'
       # ./lib/ransack/nodes/grouping.rb:95:in `block in groupings='
       # ./lib/ransack/nodes/grouping.rb:94:in `each'
       # ./lib/ransack/nodes/grouping.rb:94:in `groupings='
       # ./lib/ransack/search.rb:106:in `method_missing'
       # ./spec/ransack/search_spec.rb:687:in `block (3 levels) in <module:Ransack>'

Finished in 0.71952 seconds (files took 1.28 seconds to load)
399 examples, 101 failures, 1 pending

When I just replace "table_name" with "name" in that specific line, I can reduce the failures to 18 of the following:

  18) Ransack::Search#method_missing allows chaining to access nested conditions
      Failure/Error: raise "No table named #{table} exists."
      
      RuntimeError:
        No table named children_people exists.
      # ./lib/ransack/adapters/active_record/context.rb:19:in `type_for'
      # ./lib/ransack/nodes/attribute.rb:35:in `type'
      # ./lib/ransack/nodes/condition.rb:266:in `default_type'
      # ./lib/ransack/nodes/condition.rb:25:in `extract'
      # ./lib/ransack/nodes/grouping.rb:175:in `write_attribute'
      # ./lib/ransack/nodes/grouping.rb:155:in `block in build'
      # ./lib/ransack/nodes/grouping.rb:150:in `each'
      # ./lib/ransack/nodes/grouping.rb:150:in `build'
      # ./lib/ransack/nodes/grouping.rb:146:in `new_grouping'
      # ./lib/ransack/nodes/grouping.rb:95:in `block in groupings='
      # ./lib/ransack/nodes/grouping.rb:94:in `each'
      # ./lib/ransack/nodes/grouping.rb:94:in `groupings='
      # ./lib/ransack/search.rb:106:in `method_missing'
      # ./spec/ransack/search_spec.rb:687:in `block (3 levels) in <module:Ransack>'

So it seems, that the schema_cache is not including the table names, after the change. Since the schema_cache seems to be maintained by ActiveRecord, I'm not sure if this is a bug in ransack or Rails?

Would be happy tp help further, but I think I'm missing some context or input here.

Thanks for any further support.

Edit: You can find the current changes here.

nlsrchtr avatar May 08 '23 10:05 nlsrchtr

Tested this locally on rails 7.1 with the change to name and it works. The issue is, schema_cache is not working correctly here.

Loading development environment (Rails 7.1.0.alpha)
>> ActiveRecord::Base.connection_pool.schema_cache.send(:data_source_exists?, "posts")
=> true

RailsCod3rFuture avatar Jun 07 '23 18:06 RailsCod3rFuture

Thanks for documenting this, saved me a bunch of debugging.

Until resolved, to save anyone else a headache, I threw this in a ransack.rb in config/initializers

class Arel::Table
  def table_name
    name
  end
end

Enjoy.

aviflombaum avatar Aug 08 '23 16:08 aviflombaum

I’ve been running this branch for a few days in production and it works well.

rickychilcott avatar Oct 12 '23 11:10 rickychilcott

This got fixed by https://github.com/activerecord-hackery/ransack/commit/eac3c37d3d9a22179071dd150f01e7a5bfaefa9c

cryptomilk avatar Feb 28 '24 12:02 cryptomilk