Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7046755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:41:18+00:00 2026-05-28T02:41:18+00:00

models/host.rb class Host < ActiveRecord::Base has_many :report, :dependent => :delete_all end models/report.rb class Report

  • 0

models/host.rb

class Host < ActiveRecord::Base
   has_many :report, :dependent => :delete_all
end

models/report.rb

class Report < ActiveRecord::Base
   belongs_to :host
end

Now I want to select all Hosts with their reports, including Hosts that haven’t got any report.

I tried to achieve this by

list=Host.all(:joins => "left join `reports` on reports.host_id=host.id")

but it seems like Rails only selects fields from the hosts Table. So I am not able to access the report object with list[0].report.
In addition i would be much more happy doing the select without using raw SQL commands – in a more ruby-like manner.

How to get both, the host and the report in just one select and without using SQL?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:41:18+00:00Added an answer on May 28, 2026 at 2:41 am

    You can do

    Host.eager_load(:reports)
    

    Which will join the reports table and instantiated all the report objects. You can the use the loaded hosts and reports exactly as if they were being loaded on demand

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my models class Auction belongs_to :item belongs_to :user, :foreign_key => :current_winner_id has_many :auction_bids end
class Service < ActiveRecord::Base establish_connection( :adapter => mysql, :host => myip, :username => myusername,
Consider the following Django models: class Host(models.Model): # This is the hostname only name
DECOM_CHOICES = ( ('N', 'No'), ('Y', 'Yes'), ) class Host(models.Model): hostname = models.CharField(max_length=36, unique=True)
I have the following in my models.py: class HostData(models.Model): Manager = models.ForeignKey(Managers) Host =
Models (disregard typos / minor syntax issues. It's just pseudo-code): class SecretModel(models.Model): some_unique_field =
Example models: class Parent(models.Model): name = models.CharField() def __unicode__(self): return self.name class Child(models.Model): parent
I have the following model class DNS(models.Model): domain = models.ForeignKey(Domain) host_start = models.CharField(max_length=150, blank=True,
I have two simple models in models.py: Service and Host. Host.services has a m2m
I have a model that looks like the following: class LibraryEntry(models.Model): host_lib_song_id = models.IntegerField()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.