Last active
August 29, 2015 13:57
-
-
Save bryanbibat/9384941 to your computer and use it in GitHub Desktop.
joining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@companies = Company.includes(:users).all | |
@companies.each do |company| | |
company.users.each do |user| | |
"#{company.name} #{user.name}" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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