Hi Jeremy,
I noticed something today, and I'm wondering if maybe I'm misunderstanding
it...
I've got an association like this:
class Book < Sequel::Model
one_to_many :chapters
# has attribute "title"
end
class Chapter < Sequel::Model
many_to_one :book
# also has attribute title
def full_title
book.title + title
end
end
This is a somewhat simplified example but, you get the idea.
What I found was that if I loaded a book, for example:
book = Book.with_pk! 123
Then I loop through its chapters:
book.chapters.all.each{|c| puts c.full_title }
That I'm getting a query for every chapter that looks like this:
SELECT * FROM "books" WHERE ("id" = 123) ORDER BY "id" LIMIT 1
Hence, my conclusion that when a book loads its chapters via
`book.chapters` or `book.chapters.all` it does not pass a reference to
itself into the children, and each child has to look up the parent in order
to use the reference.
On a whim I tried doing `Book.where(id:
123).eager(:chapters).first.chapters...` to see if that would change
things, but I still see an N+1 in the query logs.
So, am I interpreting that correctly? And if so, is there a mechanism by
which I can load the children of a model and pass them a reference to the
parent so they don't query for it?
Cheers,
Andrew
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.