Guest User

Untitled

a guest
Jun 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. # I already have users and students.
  2. # I want to make an association between users and students.
  3. # Both have a column ("codigo") which have values for the ones that should be associated.
  4. # I have the following:
  5.  
  6. users = User.all
  7. students = Student.all
  8. users.each do |user|
  9. students.each do |student|
  10. if user.codigo == student.codigo
  11. user << student
  12. end
  13. end
  14. end
  15.  
  16.  
  17. # bitsweat provided me with this piece of code yesterday but I keep getting this:
  18. # Student(#17477390) expected, got NilClass(#117210)
  19.  
  20. users = User.all
  21. students = Student.all
  22. by_codigo = students.group_by(&:codigo)
  23. users.each do |u|
  24. u.students << by_codigo[u.codigo]
  25. end
Add Comment
Please, Sign In to add comment