Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draper forces DB queries on decorated model's associations #646

Closed
swiercz opened this issue Dec 19, 2014 · 8 comments · Fixed by #932
Closed

Draper forces DB queries on decorated model's associations #646

swiercz opened this issue Dec 19, 2014 · 8 comments · Fixed by #932

Comments

@swiercz
Copy link

swiercz commented Dec 19, 2014

Assume:
User has_many posts
Post has_many comments

When fetching users from DB with preloading of posts and comments:

@users = User.where(....).includes(posts: [:comments]).decorate

The AR properly preloads users, posts and comments (3 queries) but decorated users' collection forces new DB queries when rendering view.

It seems that objects from decorated collection ignore the fact that associations are preloaded by AR and fore new DB query for each one. This results with a performance drop.

RABL:

child @users do
  extends 'users/_base'
end

User partial

attributes :email
child :posts do
  extends 'posts/_base'
end

Post partial

attributes :title
child :posts do
  extends 'comments/_base'
end

Comment partial

attributes :text, :created_at

User decorator

class UserDecorator < Draper::Decorator
  delegate_all
  def posts
    object.posts.decorate
  end
end

Post decorator

class PostDecorator < Draper::Decorator
  delegate_all
  def comments
    object.comments.decorate
  end
end
@dimroc
Copy link

dimroc commented Feb 3, 2015

👍

We're pretty aggressive with our eager loading so this is causing a pretty big hit to perf on certain views.

Decorating the individual elements rather than the collection avoids this issue.

@jtomaszewski
Copy link

jtomaszewski commented Apr 18, 2016

Has this been fixed? I'm experiencing the same issue as @dimroc on draper 2.1.0.

I needed to use the same workaround, to avoid the problem:

Decorating the individual elements rather than the collection avoids this issue.

@darrinholst
Copy link

darrinholst commented Feb 15, 2017

I just noticed this as well. @jcasimir sorry for the 2 year old question, but do you remember why this was closed?

@seanlinsley
Copy link
Member

Given that there isn't a commit or PR linked to this ticket, I suspect it's still an issue.

@seanlinsley seanlinsley reopened this Feb 22, 2017
@codebycliff codebycliff added this to the 3.0.0 milestone Apr 5, 2017
@codebycliff codebycliff added the bug label Apr 5, 2017
@codebycliff
Copy link
Collaborator

I was able to reproduce this behavior and I fixed it by using decorates_associated instead of overriding the method:

class UserDecorator < Draper::Decorator
  delegate_all
  decorates_associated :posts
end

class PostDecorator < Draper::Decorator
  delegate_all
  decorates_associated :comments
end

Were the rest of you experiencing this issue also overriding the association method in the decorator? If so, you should be able to change it to the code above. If there is another scenario that is causing this, please let me. If you could provide an example app, that would be even better.

@dimroc
Copy link

dimroc commented Apr 11, 2017 via email

@darrinholst
Copy link

I couldn't find the part of our app that I was seeing this in now. It appeared to be working where I thought the problem was now. It was a low traffic part so I guess I didn't bother to document it anywhere. I'd call it resolved for now. I'll make sure to get more details if/when I see it again.

@codebycliff
Copy link
Collaborator

Okay, I'm going to close this then. Thanks for the responses.

Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 4, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 4, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 4, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 4, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
@Alexander-Senko Alexander-Senko modified the milestones: 3.0, 4.0.3 Sep 4, 2024
@Alexander-Senko Alexander-Senko linked a pull request Sep 4, 2024 that will close this issue
1 task
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 4, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 7, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 7, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 7, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit to Alexander-Senko/draper that referenced this issue Sep 7, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves drapergem#646,
         drapergem#706,
         drapergem#827.
Alexander-Senko added a commit that referenced this issue Sep 13, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves #646,
         #706,
         #827.
Alexander-Senko added a commit that referenced this issue Oct 4, 2024
`ActiveRecord::Associations::CollectionProxy#decorate` ignores `target`
(be it already loaded or not) and loads the association again every time
it's called.

That's why unsaved records get missing from the decorated collection.

Resolves #646,
         #706,
         #827.

(cherry picked from commit c7e37c1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants