ruby Ransack带 predicate 的未定义方法

m4pnthwp  于 2023-10-17  发布在  Ruby
关注(0)|答案(1)|浏览(150)
Rails 7.1.0.beta1
ransack 4

控制器:

@q = Customer.ransack(params[:q])
@customers = @q.result(distinct: true)

检视:

<%= search_form_for @q do |f| %>
    <%= f.label :name %>
    <%= f.search_field :name_cont %>

    <%= f.submit %>
  <% end %>
  <% @customers.each do |customer| %>
    <%= render customer %>
    <p>
      <%= link_to "Show this customer", customer %>
    </p>
  <% end %>

错误输出:
未定义的方法`table_name' for #<Arel::Table:0x00007f3f8a7612d8@name =“customers”,@klass=Customer(id:,品名:,到岸价:,地址:,邮政编码:,town:,created_at:,updated_at:),@type_caster=#<ActiveRecord::TypeCaster::Map:0x00007f3f8a9547e8@klass =Customer(id:,品名:,到岸价:,地址:,邮政编码:,town:,created_at:,updated_at:)>,@table_alias=nil>
跟踪:

[ransack (4.0.0) lib/ransack/adapters/active_record/context.rb:16:in `type_for'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/nodes/attribute.rb:35:in `type'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/nodes/condition.rb:266:in `default_type'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/nodes/condition.rb:25:in `extract'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/nodes/grouping.rb:175:in `write_attribute'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/nodes/grouping.rb:115:in `method_missing'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/search.rb:54:in `block in build'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/search.rb:48:in `each'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/search.rb:48:in `build'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/search.rb:40:in `initialize'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/adapters/active_record/base.rb:16:in `new'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[ransack (4.0.0) lib/ransack/adapters/active_record/base.rb:16:in `ransack'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/abstract_controller/base.rb:224:in `process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal/rendering.rb:165:in `process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/abstract_controller/callbacks.rb:259:in `block in process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/callbacks.rb:121:in `block in run_callbacks'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actiontext/lib/action_text/rendering.rb:23:in `with_renderer'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actiontext/lib/action_text/engine.rb:69:in `block (4 levels) in <class:Engine>'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/callbacks.rb:130:in `instance_exec'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/callbacks.rb:130:in `block in run_callbacks'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/callbacks.rb:141:in `run_callbacks'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/abstract_controller/callbacks.rb:258:in `process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal/rescue.rb:25:in `process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal/instrumentation.rb:74:in `block in process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/notifications.rb:206:in `block in instrument'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/notifications/instrumenter.rb:58:in `instrument'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/notifications.rb:206:in `instrument'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal/instrumentation.rb:73:in `process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal/params_wrapper.rb:261:in `process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activerecord/lib/active_record/railties/controller_runtime.rb:32:in `process_action'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/abstract_controller/base.rb:160:in `process'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionview/lib/action_view/rendering.rb:40:in `process'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal.rb:227:in `dispatch'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_controller/metal.rb:309:in `dispatch'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/routing/route_set.rb:49:in `dispatch'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/routing/route_set.rb:32:in `serve'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/journey/router.rb:51:in `block in serve'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/journey/router.rb:32:in `each'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/journey/router.rb:32:in `serve'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/routing/route_set.rb:882:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack (3.0.8) lib/rack/tempfile_reaper.rb:20:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack (3.0.8) lib/rack/etag.rb:29:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack (3.0.8) lib/rack/conditional_get.rb:31:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack (3.0.8) lib/rack/head.rb:15:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/http/permissions_policy.rb:36:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/http/content_security_policy.rb:33:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack-session (2.0.0) lib/rack/session/abstract/id.rb:272:in `context'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack-session (2.0.0) lib/rack/session/abstract/id.rb:266:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/cookies.rb:689:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activerecord/lib/active_record/migration.rb:653:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/callbacks.rb:101:in `run_callbacks'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/callbacks.rb:28:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/executor.rb:14:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/actionable_exceptions.rb:16:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:29:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[web-console (4.2.1) lib/web_console/middleware.rb:132:in `call_app'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[web-console (4.2.1) lib/web_console/middleware.rb:19:in `block in call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[web-console (4.2.1) lib/web_console/middleware.rb:17:in `catch'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[web-console (4.2.1) lib/web_console/middleware.rb:17:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) railties/lib/rails/rack/logger.rb:37:in `call_app'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) railties/lib/rails/rack/logger.rb:24:in `block in call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/tagged_logging.rb:135:in `block in tagged'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/tagged_logging.rb:39:in `tagged'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/tagged_logging.rb:135:in `tagged'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) railties/lib/rails/rack/logger.rb:24:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[sprockets-rails (3.4.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/remote_ip.rb:92:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/request_id.rb:28:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack (3.0.8) lib/rack/method_override.rb:28:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack (3.0.8) lib/rack/runtime.rb:24:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/server_timing.rb:59:in `block in call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/server_timing.rb:24:in `collect_events'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/server_timing.rb:58:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/executor.rb:14:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/static.rb:25:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rack (3.0.8) lib/rack/sendfile.rb:114:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) actionpack/lib/action_dispatch/middleware/host_authorization.rb:141:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[rails (d56db8ff17cf) railties/lib/rails/engine.rb:529:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[puma (6.4.0) lib/puma/configuration.rb:272:in `call'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[puma (6.4.0) lib/puma/request.rb:100:in `block in handle_request'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[puma (6.4.0) lib/puma/thread_pool.rb:378:in `with_force_shutdown'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[puma (6.4.0) lib/puma/request.rb:99:in `handle_request'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[puma (6.4.0) lib/puma/server.rb:443:in `process_client'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[puma (6.4.0) lib/puma/server.rb:241:in `block in run'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
[puma (6.4.0) lib/puma/thread_pool.rb:155:in `block in spawn_thread'](http://localhost:9999/customers?q%5Bname%5D=asdf&commit=Search#)
iqjalb3h

iqjalb3h1#

ransack尚未更新到7.1。版本4.0.0文档状态:
Ransack在Ruby 2.7和更高版本的Rails 7.0、6.1中得到支持。
你的问题尤其是这一行:

table = attr.arel_attribute.relation.table_name

attr.arel_attribute.relation将返回Arel::Table
在Rails 7.0和之前的版本中,Arel::Table#table_nameArel::Table#name的别名;然而在7.1中,这是removed,因为该方法从未在Rails代码库中使用过。
此时,您有3个选择:
1.降级到7.0.X轨道;或
1.等待ransack更新他们的代码库以支持7.1;或
1.修补Arel::Table以将这一行放回,例如

module Arel
  class Table
    alias :table_name :name
  end 
end

就我个人而言,我会选择#3,因为这是最直接的,它是向后兼容的,否则不会破坏和影响。

相关问题