I am triying to optimize the following due to slow work on method *select* from ruby into more than 16.000 records, but i really dont know how to build it into SQL query or using joins and includes:

```
User.invited.not_rejected.order('users.updated_at desc').select{|x| x unless x.rtr.nil?}

```

The relations are:

```
class User < ApplicationRecord
  hass_many :transition_elements
  has_many :rtrs, through: :transition_elements

  scope :not_rejected -> { where(rejected: false) }
  scope :invited -> { where(invited: true) }
end

class TransitionElement < ApplicationRecord
  has_many :rtrs
  belongs_to :user
end

class Rtr < ApplicationRecord
  belongs_to :transition_element
end

```