Skip to content

Instantly share code, notes, and snippets.

@bryanbibat
Last active August 29, 2015 13:57
Show Gist options
  • Save bryanbibat/9384941 to your computer and use it in GitHub Desktop.
Save bryanbibat/9384941 to your computer and use it in GitHub Desktop.
joining
@companies = Company.includes(:users).all
@companies.each do |company|
company.users.each do |user|
"#{company.name} #{user.name}"
end
end
@companies = Company.find_by_sql <<-SQL
SELECT c.name AS company_name, u.name AS user_name
FROM companies c
INNER JOIN users u ON c.id = u.company_id
SQL
@companies.each do |company|
"#{company.company_name} #{company.user_name}"
end
@companies = Company.select("companies.name AS company_name, users.name AS user_name").joins(:users).all
@companies.each do |company|
"#{company.company_name} #{company.user_name}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment