-
|
Hi 👋 I'm working with multi-table inheritance and I’d like to iterate over all events in one pass while avoiding N+1 queries and still being able to branch on the concrete subtype. Models: class Event < Marten::Model
field :id, :big_int, primary_key: true, auto: true
field :title, :string, max_size: 255
field :description, :text
field :start_time, :date_time
field :end_time, :date_time, null: true
field :all_day, :bool, default: false
field :timezone, :string, max_size: 100, default: "UTC"
field :location_name, :string, max_size: 255
field :location_address, :string, max_size: 255
field :online_link, :string, max_size: 255, null: true
end
class ConcertEvent < Event
field :artist, :string, max_size: 255
field :genre, :string, max_size: 100
field :venue_capacity, :int
end
class WorkshopEvent < Event
field :instructor, :string, max_size: 255
field :topic, :string, max_size: 255
field :duration_hours, :int
endGoal
Minimal code:events = Event.all
events.each do |event|
puts "Event: #{event.title}, Start: #{event.start_time}, End: #{event.end_time}"
if (concert = event.concert_event)
puts "Type: Concert (artist=#{concert.artist}, capacity=#{concert.venue_capacity})"
elsif (workshop = event.workshop_event)
puts "Type: Workshop (instructor=#{workshop.instructor}, hours=#{workshop.duration_hours})"
else
puts "Type: Base Event"
end
endObserved behavior:
What I tried to mitigate N+1:
Questions
I hope I've gathered the most relevant details. If not feel free to ask. Thanks for reading! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hey! 👋
What you were trying to do in one of your examples (joining all the needed child reverse relations) should work in theory: events = Event.all.join(:concert_event, :workshop_event)Unfortunately, this doesn't work right now. 😢 The fact that it doesn't is certainly not intended and should be fixed. EDIT: fixed in e29ec6d.
That's an idea, but I would argue that this should never be the default behavior. I see at least two reasons for that:
|
Beta Was this translation helpful? Give feedback.
Hey! 👋
What you were trying to do in one of your examples (joining all the needed child reverse relations) should work in theory:
Unfortunately, this doesn't work right now. 😢 The fact that it doesn't is certainly not intended and should be fixed.
EDIT: f…