Home > database >  Ruby NoMethodError: undefined method
Ruby NoMethodError: undefined method

Time:05-02

I am using rack-reducer with 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.

CodePudding user response:

You should set class method by 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
  • Related