As recommended by Carvalho paste your code into texts. It will be much simpler to help you, and you will get help easier and quicker if you do it the right way.
About your code. One of the things I'm fixing is that you're using SQL to scan the entire table and then do the processing of it. Try to minimize bank efforts in the array.
Come on, I would trade these methods for it
def students
works_students.where.not(student: nil)
end
If you have this same concept of students in other models, your performance problem will surely be there. However, if it is only this, then there are probably more items to be analyzed.
def program_id
Qrcode.find_by(code: self.teacher_qrcode).try(:program_id)
end
I do not know about the rest. I'd have to take a closer look at your implementation.
I do not know how to create an association of has_one :qrcode
in model
teacher?
Having an association you could directly access the code by the controller like this:
work.teacher.qrcode.program_id