Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Preload, Eagerload, Includes and Joins
user_id = 11

Task.joins("left JOIN ownerships ON ownerships.subject_id = tasks.id AND ownerships.subject_type = 'Task'").where("ownerships.subject_type" => "Task", user_id: 11).select("distinct '#{subject_klass.table_name}.id'").select("#{subject_klass.table_name}.*")

Task.joins("left JOIN ownerships ON ownerships.subject_id = tasks.id AND ownerships.subject_type = 'Task'").where("ownerships.subject_type" => "Task", user_id: 11).group("#{subject_klass.table_name}.id")

Task.includes(:ownerships).where("ownerships.subject_type" => "Task", user_id: 11)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment