Author has_many books. What does this query return?Author.joins(:books).where(books: {published: true}).distinctjoins and where do together.joins creates an INNER JOIN, so only authors with matching books appear. The where filters books to published ones. distinct removes duplicate authors if they have multiple published books.
Post.includes(:comments).where(comments: {approved: true}), what kind of SQL query does Rails generate?includes works with conditions on associated tables.includes loads posts and associated comments in separate queries by default. When filtering on associated table columns, Rails converts to a JOIN automatically, so it runs a single query with INNER JOIN.
User.includes(:profile).where(profiles: {active: true})Why?
When using includes with conditions on the included table, Rails needs references(:profile) to generate a proper JOIN. Without it, it raises an error because it cannot resolve the table in SQL.
Order records with their items, but only items with status: 'shipped'. Which code is correct?eager_load forces a LEFT OUTER JOIN and eager loads associations in one query, allowing filtering on associated tables. includes alone may not generate the correct SQL.
joins and includes in Rails?joins and includes:joins creates an INNER JOIN and filters records based on join conditions. includes is for eager loading to prevent N+1 queries, and may use multiple queries or LEFT OUTER JOIN depending on usage.
