
Eager loading associations related to user
Why make this post It was a problem i faced myself with and couldn't find any real direct answer to. "How do i eager load if a user has liked a post? If a user is already friends with another user?" . I wanted to know how to eager load if an association that is directly related to the user exists(though it can be used in other ways, not just this one) and that now has lead me to write this so that other people that come across the same problem might find a possible answer here. In this example we will eager load posts alongside the user's like. Current Attributes To be able to know if an Active Record object is related to an user we first need to share some kind of unique data related to the user with the model. Enters current attributes. class Current < ActiveSupport :: CurrentAttributes attribute :user_id end (Should be a file called Current inside models) We make use of the Current class to make the user id available globally, we could make the whole user object available but we onl
Continue reading on Dev.to
Opens in a new tab



