Skip to content

Instantly share code, notes, and snippets.

@halloffame
Created November 20, 2012 19:14
Show Gist options
  • Save halloffame/4120338 to your computer and use it in GitHub Desktop.
Save halloffame/4120338 to your computer and use it in GitHub Desktop.
Problem doing a joins in signups
# Problem: doing a .joins(:user) when using mySQL returns an empty array even when signups exist. The reason I am doing a joins is so that I can sort by user name.
### works in SQLlite ###
>>> o.signups.joins(:user).order('users.first_name ASC')
Signup Load (1.3ms) SELECT "signups".* FROM "signups" INNER JOIN "users" ON "users"."id" = "signups"."user_id" WHERE "signups"."occurrence_id" = 8 ORDER BY users.first_name ASC
=> [#<Signups ...>]
>>> o.signups
Signup Load (1.9ms) SELECT "signups".* FROM "signups" WHERE "signups"."occurrence_id" = 8
=> [#<Signups ...>]
### Doesn't work in mySQL ###
>>> o.signups.joins(:user).order('users.first_name ASC')
Signup Load (1.7ms) SELECT `signups`.* FROM `signups` INNER JOIN `users` ON `users`.`id` = `signups`.`user_id` WHERE `signups`.`occurrence_id` = 8 ORDER BY users.first_name ASC
=> [] ### returning empty array, even when i do just .joins(:user) and leave out the order part
>>> o.signups
Signup Load (0.7ms) SELECT `signups`.* FROM `signups` WHERE `signups`.`occurrence_id` = 8
=> [#<Signups ...>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment