Ruby NoMethodError: undefined method
Ruby NoMethodError: undefined method
我正在使用 rack-reducer
和 rails
。我收到错误:
#<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
我试过将方法移动到 class 的顶部,但没有效果。
您应该通过 def self.
设置 class 方法
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
我正在使用 rack-reducer
和 rails
。我收到错误:
#<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
我试过将方法移动到 class 的顶部,但没有效果。
您应该通过 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