[英]Ruby NoMethodError: undefined method
I am using rack-reducer
with rails
.我正在使用
rack-reducer
和rails
。 I am getting the error:我收到错误:
#<NoMethodError: undefined method 'order_from_user_input' for #<DataPoint::ActiveRecord_Relation:0x00007fdfac022f90>>
class DataPoint < ApplicationRecord
Reducer = Rack::Reducer.new(
self.all,
->(limit: 150) { limit(limit) },
->(offset: 0) { offset(offset) },
->(order:) {order(id: order_from_user_input(order))}
)
def order_from_user_input direction
if direction.lower == 'asc'
return 'ASC'
elsif direction.lower == 'desc'
return 'DESC'
else
return 'ASC'
end
end
end
I have tried moving the method to the top of the class, but that had no effect.我曾尝试将该方法移至 class 的顶部,但没有任何效果。
You should set class method by def self.
您应该通过
def self.
class DataPoint < ApplicationRecord
Reducer = Rack::Reducer.new(
self.all,
->(limit: 150) { limit(limit) },
->(offset: 0) { offset(offset) },
->(value: 'asc') { order(id: order_by_direction(value)) }
)
def self.order_by_direction(value)
return :desc if value.lower == 'desc'
:asc
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.