I have this query :
city.city_combat_units.includes(:combat_unit).where('combat_units.name = ?', 'unit name')
A city_combat_unit is a combat_unit that belongs to a city. A combat_unit has a name attribute. A city_combat_unit has a city_id and a number_of_units attribute:
CombatUnit
name
City
CityCombatUnit
city_id
combat_unit_id
number_of_units
What i want to do is get all those city combat units for a city that have a specific name. The best i’ve come up with(in a single line) is the above code, but i fear that i am missing on something way simpler. Am i ?
Try setting up a has_many :through relationship
Then you should be able to do something like this