ruby on rails - Eager loading a custom join with an instance variable -
given system allows users invite other users events:
class event has_many :invites end class user has_many :invites has_many :invited, inverse_of: :inviter, foreign_key: :inviter_id, class_name: 'invite' end class invite belongs_to :user belongs_to :event belongs_to :inviter, class_name: 'user' has_many :invited, -> (invite) { where(invites: { event_id: invite.event_id }) }, through: :user, class_name: 'invite' end
i'm generating simple report shows invites event. i'm trying modify report include actions can done against invited
sample event:
<%- event.includes(:user).each |invite| -%> <%= invite.user.name %> <%- invite.invited.each |other| -%> <%= link_to invite_path(other), method: :destroy %> ... <% end %> <%- end -%> <%- end -%>
this works fine has n+1 issue. attempting eager load custom association gives:
deprecation warning: association scope 'invited' instance dependent (the scope block takes argument). preloading happens before individual instances created. means there no instance being passed association scope. result in broken or incorrect behavior. joining, preloading , eager loading of these associations deprecated , removed in future.
is there way eager loading this? possible give other hints has_many
instead of using instance variable?
see rails 5 how form association between tables on multiple shared attributes.
you may need define 2 different associations. 1 instance-specific , other use during joins.
Comments
Post a Comment