I have two models – Topic and ContentItems.
A topic has_many content_items but a topic can be created without any pre-existing content_items.
Does anyone know how I can select all Topics with at least one content_item without using a counter cache?
currently I’m doing something like:
Topic.joins(:content_items).uniq
but that handles it in memory so if I can’t chain a limit or order off of it i.e. I can’t do
Topic.joins(:content_items).uniq.limit(10).order("created_at DESC")
Counter cache is a possible long term option but it’s a bit larger of a change than I’d like to make right now unless there are no better solutions.
Thanks all
You can try this
OR