Hey,
I love ROM and I am curious about the above question: What’s the best way to load belongs_to-assocation in one select?
# states-relation
module Orm
module Relations
class States < ROM::Relation[:sql]
schema(:states, infer: true)
end
end
end
# records-relation
module Orm
module Relations
class Records < ROM::Relation[:sql]
schema(:records, infer: true) do
associations do
belongs_to :states, as: :status, foreign_key: :status_id
end
end
end
end
end
Following solution works, but 2 selects are executed. I think in this case it is more performant to combine the states in one select. Is this possible? With plain-sequel there is eager_graph.
# records-repo
module Repositories
class AiRecordsRepo < ROM::Repository[:records]
include Import["orm.container"]
struct_namespace Entities
def all_with_status
ai_records.combine(:status).to_a
end
end
end
Or is the only way I can achieve this the following:
def all_with_status
ai_records.join(:states).select_append(states[:name]).to_a
end
The disadvantage of the select_append solution is, that the real association is not loaded.
What’s your opinion about that?
UPDATE#########################
I came across ROM:Relation#wrap. It solves my use-case? It is not documented at first glance on the main webpage. Is this a solution you would use? Maybe we should add it to the web-documentation.
def all_with_status
# ai_records.join(:states).select_append(states[:name]).to_a
ai_records.wrap(:status).to_a
end
UPDATE########################
Thanks!
Best regards
– Armin