Есть 3 модели:
class Course < ApplicationRecord
has_many :tickets
has_many :users, through: :tickets
end
class Ticket < ApplicationRecord
belongs_to :user
belongs_to :course
end
class User < ApplicationRecord
has_many :tickets
has_many :courses, through: :tickets
end
В Ticket есть поле :status
.
Мне нужно выбрать все курсы пользователя, у которых в ticket со статусом 'paid'.
Вот рабочий код:
def paid
@courses = []
paid_tickets = current_user.tickets.where(status: 'paid')
paid_tickets.each { |ticket| @courses << ticket.course }
@courses
end