ruby-on-railsactiverecordscoperuby-on-rails-5instance-methods

Using an instance method in scope returns incorrect result


user.rb

has_many :users_positions
has_many :positions, through: :users_positions
//users table has position_id as a field, it represents the current position user is associated with

scope :under_25_value, -> { joins(:positions).where('(value * positions.available / 100) in (?)', 0..24) }
scope :under_50_value, -> { joins(:positions).where('(value * positions.available / 100) in (?)', 25..49) }
scope :over_50_value, -> { joins(:positions).where('(value * positions.available / 100) >= ?', 50) }

def current_positions
  value * position.available / 100
end

In the rails console, if I try the below command it returns users who have current_positions as 75 but it should have returned under 25.

User.under_25_value

Please help me find where I am going wrong

Sample Data

User id: 894, name: "Jack", value: 18, position_id: 3
Position id: 3, available: 100, name: 'Manager'

Solution

  • Since your user model already has a position_id column that points to the positions you want to query for each user, you can define a position association and update your code like below:

    user.rb

    has_many :users_positions
    has_many :positions, through: :users_positions
    //users table has position_id as a field, it represents the current position user is associated with
    has_one :position
    
    scope :under_25_value, -> { joins(:position).where('(value * positions.available / 100) in (?)', 0..24) }
    scope :under_50_value, -> { joins(:position).where('(value * positions.available / 100) in (?)', 25..49) }
    scope :over_50_value, -> { joins(:position).where('(value * positions.available / 100) >= ?', 50) }
    
    def current_positions
      value * position.available / 100
    end