ActiveRecord::BelongsToIf
Allows you to manage preload condition by :if
condition.
Usage
class User < ActiveRecord::Base
has_many :activities
end
class Activity < ActiveRecord::Base
belong_to :comment, if: -> { activity_type == "Comment" }
belong_to :issue, if: -> { activity_type == "Issue" }
belong_to :pull_request, if: -> { activity_type == "PullRequest" }
end
user.activities.includes(
[
:comment,
{
issue: :owner,
pull_request: :owner,
},
]
)