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

model.assocations.decorate loads the association even if it is preloaded (AR) #827

Closed
PragTob opened this issue Dec 11, 2017 · 3 comments · Fixed by #932
Closed

model.assocations.decorate loads the association even if it is preloaded (AR) #827

PragTob opened this issue Dec 11, 2017 · 3 comments · Fixed by #932
Assignees
Milestone

Comments

@PragTob
Copy link

PragTob commented Dec 11, 2017

Hi there,

first and foremost - thanks for draper 🎉

In our application I run into the problem that I want to explicitly decorate an association not as part of the decorator definition (sadly this easily leads to performance problems) so we want to do model.association.decorate but this seems to reload the objects from the database even if they were preloaded before.

See this snippet:

[13] pry(main)> ts = TourSchedule.includes(:tour_schedule_plans).find(104281); nil
  TourSchedule Load (1.0ms)  SELECT  "tour_schedules".* FROM "tour_schedules" WHERE "tour_schedules"."id" = $1 LIMIT $2  [["id", ID], ["LIMIT", 1]]
  TourSchedulePlan Load (1.3ms)  SELECT "tour_schedule_plans".* FROM "tour_schedule_plans" WHERE "tour_schedule_plans"."tour_schedule_id" = ID
=> nil
[14] pry(main)> ts.tour_schedule_plans.loaded?
=> true
[15] pry(main)> ts.tour_schedule_plans.to_a; nil
=> nil
[16] pry(main)> ts.tour_schedule_plans.decorate.to_a; nil
  TourSchedulePlan Load (0.8ms)  SELECT "tour_schedule_plans".* FROM "tour_schedule_plans" WHERE "tour_schedule_plans"."tour_schedule_id" = $1  [["tour_schedule_id", ID]]
=> nil

Draper version in use is 3,0.0.

Somewhat related issues (associations and preloading) are:

edit: FYI mapping over all objects and decorating them instead seems to be a fine workaround as indicated by early testing ts.tour_schedule_plans.map &:decorate

Thanks!

@brunohkbx
Copy link
Contributor

@codebycliff I don't think there is any way to avoid this query when .decorate calls .all even if it was preloaded 🤔
https://github.com/drapergem/draper/blob/master/lib/draper/decoratable.rb#L58

@Alexander-Senko Alexander-Senko self-assigned this Sep 4, 2024
@Alexander-Senko Alexander-Senko added this to the 4.0.3 milestone Sep 4, 2024
@Alexander-Senko
Copy link
Collaborator

Duplicates #646. The reason is the same as the one for #706.

@Alexander-Senko
Copy link
Collaborator

@brunohkbx, thanks for the point 👍

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 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.

3 participants