So, I have trouble with
Preloading instance dependent scopes are not supported.
I have three models
class P < ApplicationRecord
has_many :as
has_many :cs
end
class C < ApplicationRecord
belongs_to :p
end
class A < ApplicationRecord
belongs_to :p
has_one :c, -> (a) { where(feature: a.feature) }, through: :p, source: :cs
end
And i have three fast_jsonapi serializers
class PSerializer
include FastJsonapi::ObjectSerializer
has_many :as
end
class CSerializer
include FastJsonapi::ObjectSerializer
belongs_to :p
end
class ASerializer
include FastJsonapi::ObjectSerializer
belongs_to :p
has_one :c
end
And this one seed file
p_model = P.create(title: 'PTitle')
4.times do |i|
A.create(title: "aTitle-#{i}", feature: "feature-#{i}", p: p_model)
C.create(title: "cTitle-#{i}", feature: "feature-#{i}", p: p_model)
end
I want to render P with his A and A’s C’s But when i try to do
PSerializer.new(P.first, { include: [:as, :'as.c'] }).serialized_json
i got
P Load (0.1ms) SELECT "ps".* FROM "ps" ORDER BY "ps"."id" ASC LIMIT ? [["LIMIT", 1]]
(0.1ms) SELECT "as"."id" FROM "as" WHERE "as"."p_id" = ? [["p_id", 1]]
A Load (0.1ms) SELECT "as".* FROM "as" WHERE "as"."p_id" = ? [["p_id", 1]]
C Load (0.2ms) SELECT "cs".* FROM "cs" INNER JOIN "ps" ON "cs"."p_id" = "ps"."id" WHERE "ps"."id" = ? AND "cs"."feature" = ? LIMIT ? [["id", 1], ["feature", "feature-0"], ["LIMIT", 1]]
C Load (0.1ms) SELECT "cs".* FROM "cs" INNER JOIN "ps" ON "cs"."p_id" = "ps"."id" WHERE "ps"."id" = ? AND "cs"."feature" = ? LIMIT ? [["id", 1], ["feature", "feature-1"], ["LIMIT", 1]]
C Load (0.1ms) SELECT "cs".* FROM "cs" INNER JOIN "ps" ON "cs"."p_id" = "ps"."id" WHERE "ps"."id" = ? AND "cs"."feature" = ? LIMIT ? [["id", 1], ["feature", "feature-2"], ["LIMIT", 1]]
C Load (0.1ms) SELECT "cs".* FROM "cs" INNER JOIN "ps" ON "cs"."p_id" = "ps"."id" WHERE "ps"."id" = ? AND "cs"."feature" = ? LIMIT ? [["id", 1], ["feature", "feature-3"], ["LIMIT", 1]]
So, looks like N + 1. But i know what i can use includes to resolve it.
PSerializer.new(P.includes({ as: :c }).first, { include: [:as, :'as.c'] }).serialized_json
Oops:
irb(main):010:0> PSerializer.new(P.includes({ as: :c }).first, { include: [:as, :'as.c'] }).serialized_json
P Load (0.1ms) SELECT "ps".* FROM "ps" ORDER BY "ps"."id" ASC LIMIT ? [["LIMIT", 1]]
A Load (0.1ms) SELECT "as".* FROM "as" WHERE "as"."p_id" = ? [["p_id", 1]]
Traceback (most recent call last):
1: from (irb):10
ArgumentError (The association scope 'c' is instance dependent (the scope block takes an argument). Preloading instance dependent scopes is not supported.)
I can try with left_joins
irb(main):011:0> PSerializer.new(P.left_joins({ as: :c }).first, { include: [:as, :'as.c'] }).serialized_json
Traceback (most recent call last):
1: from (irb):11
ArgumentError (The association scope 'c' is instance dependent (the scope block takes an argument). Preloading instance dependent scopes is not supported.)
Actually the same. How I can resolve this N + 1 issue?
I created rails repo with these models, so you can try it by self. https://github.com/X1ting/reproduce_preload_bug Rails 5.2.3 Ruby 2.5.1
Many thanks to @igor-khodyrev
He advised me to use composite keys and it works! So, solution:
Add the gem to Gemfile
gem 'composite_primary_keys', '=11'
and change association in A model to this
has_one :c, foreign_key: [:p_id, :feature], primary_key: [:p_id, :feature]
N + 1 Resolved:
irb(main):005:0> PSerializer.new(P.includes({ as: :c }).first, { include: [:as, :'as.c'] }).serialized_json
P Load (0.2ms) SELECT "ps".* FROM "ps" ORDER BY "ps"."id" ASC LIMIT ? [["LIMIT", 1]]
A Load (0.1ms) SELECT "as".* FROM "as" WHERE "as"."p_id" = ? [["p_id", 1]]
C Load (0.1ms) SELECT "cs".* FROM "cs" WHERE ("cs"."p_id" = 1 AND "cs"."feature" = 'feature-0' OR "cs"."p_id" = 1 AND "cs"."feature" = 'feature-1' OR "cs"."p_id" = 1 AND "cs"."feature" = 'feature-2' OR "cs"."p_id" = 1 AND "cs"."feature" = 'feature-3' OR "cs"."p_id" = 1 AND "cs"."feature" = 'feature-kek')
=> "{\"data\":{\"id\":\"1\",\"type\":\"p\",\"relationships\":{\"as\":{\"data\":[{\"id\":\"1\",\"type\":\"a\"},{\"id\":\"2\",\"type\":\"a\"},{\"id\":\"3\",\"type\":\"a\"},{\"id\":\"4\",\"type\":\"a\"},{\"id\":\"5\",\"type\":\"a\"}]}}},\"included\":[{\"id\":\"1\",\"type\":\"a\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}},\"c\":{\"data\":{\"id\":\"1\",\"type\":\"c\"}}}},{\"id\":\"2\",\"type\":\"a\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}},\"c\":{\"data\":{\"id\":\"2\",\"type\":\"c\"}}}},{\"id\":\"3\",\"type\":\"a\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}},\"c\":{\"data\":{\"id\":\"3\",\"type\":\"c\"}}}},{\"id\":\"4\",\"type\":\"a\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}},\"c\":{\"data\":{\"id\":\"4\",\"type\":\"c\"}}}},{\"id\":\"5\",\"type\":\"a\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}},\"c\":{\"data\":null}}},{\"id\":\"1\",\"type\":\"c\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}}}},{\"id\":\"2\",\"type\":\"c\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}}}},{\"id\":\"3\",\"type\":\"c\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}}}},{\"id\":\"4\",\"type\":\"c\",\"relationships\":{\"p\":{\"data\":{\"id\":\"1\",\"type\":\"p\"}}}}]}"
I also added solution PR to my repo. https://github.com/X1ting/reproduce_preload_bug/pull/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With