Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # I already have users and students.
- # I want to make an association between users and students.
- # Both have a column ("codigo") which have values for the ones that should be associated.
- # I have the following:
- users = User.all
- students = Student.all
- users.each do |user|
- students.each do |student|
- if user.codigo == student.codigo
- user << student
- end
- end
- end
- # bitsweat provided me with this piece of code yesterday but I keep getting this:
- # Student(#17477390) expected, got NilClass(#117210)
- users = User.all
- students = Student.all
- by_codigo = students.group_by(&:codigo)
- users.each do |u|
- u.students << by_codigo[u.codigo]
- end
Add Comment
Please, Sign In to add comment