0 votes
in Ruby by

Given two models, what is the issue with the query used to fetch them?

class LineItem < ApplicationRecord

end

class Order < ApplicationRecord

  has_many :line_items

end

Order.limit(3).each { |order| puts order.line_items }

a. This query will result in extensive caching, and you will have to then deal with caching issues.

b. This query will result in the N+1 query issue. Three orders will result in four queries.

c. This query will result in the 1 query issue. Three orders will result in one query.

d. There are no issues with this query, and you are correctly limiting the number of Order models that will be loaded.

1 Answer

0 votes
by

Given two models, what is the issue with the query used to fetch them?

class LineItem < ApplicationRecord

end

class Order < ApplicationRecord

  has_many :line_items

end

Order.limit(3).each { |order| puts order.line_items }

Correct answer is :-  This query will result in the N+1 query issue. Three orders will result in four queries.

Related questions

0 votes
asked Sep 2, 2022 in Ruby by DavidAnderson
0 votes
asked Sep 3, 2022 in Ruby by DavidAnderson
...