I am looking to split my code from one “god” relation into related relations.
The split would help reliability and composability.
I will use an example with Animals, Classifications, and Categories.
class Animals < ROM::Relation[:sql]
schema(:animals, infer: true) do
associations do
belongs_to :classifications
end
end
def level_one_animals
# Here join classification with categories level 1
end
end
class Classifications < ROM::Relation[:sql]
schema(:classifications, infer: true) do
associations do
belongs_to :categories
end
end
def with_categories_level(level)
# Here join categories for level
end
end
class Categories < ROM::Relation[:sql]
schema(:categories, infer: true)
def for_level(level)
where(level: level)
end
end
How may Animals join Classification and Categories, without knowing anything about Categories?
Use whatever query makes sense in your case. Typically, you want as little db roundtrips as possible and data in a form that is either exactly what you need or very close to what you need, so that you can easily post-process it on the ruby side.
Demeter doesn’t apply here as long as you define view methods, meaning methods that always return relation objects.