To load associated relations you can simply use join, left_join, or right_join.
Using joins with relations
[:sql]
schema(infer: true) do
associations do
has_many :tasks
has_many :posts
end
end
join(tasks)
end
left_join(posts)
end
end
Using joins with explicit name and options
If you want to have more control, you can pass table name and additional options yourself:
[:sql]
schema(infer: true) do
associations do
has_many :tasks
has_many :posts
end
end
join(:tasks, user_id: :id, priority: 1)
end
left_join(:posts, user_id: :id)
end
end
Using joins with additional options
The second option hash can be used too, if you want to provide more options:
[:sql]
schema(infer: true) do
associations do
has_many :tasks
has_many :posts
end
end
join(:tasks, { user_id: :id }, table_alias: :user_tasks)
end
left_join(posts, { user_id: :id }, table_alias: :user_posts)
end
end