Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:33:26+00:00 2026-05-31T13:33:26+00:00

I have this query : city.city_combat_units.includes(:combat_unit).where(‘combat_units.name = ?’, ‘unit name’) A city_combat_unit is a

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:33:26+00:00Added an answer on May 31, 2026 at 1:33 pm

    Try setting up a has_many :through relationship

    class City < ActiveRecord::Base
      has_many :city_combat_units
      has_many :combat_units, :through => :city_combat_units
    end
    
    class CityCombatUnit < ActiveRecord::Base
      belongs_to :city
      belongs_to :combat_unit
    end
    
    class CombatUnit < ActiveRecord::Base
      has_many :city_combat_units
      has_many :cities, :through => :city_combat_units
    end
    

    Then you should be able to do something like this

    city.combat_units.where(:name => 'unit name')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this xml file <?xml version=1.0 encoding=utf-8?> <CRM> <Unit ID=1 Name=آذربایجان شرقی> <city
I have this query: Select (Country.Name + ', ' + City.Name) as Location ...
I have this query: $FullName = mysql_real_escape_string($_REQUEST['name']); $EmailAdd = mysql_real_escape_string($_REQUEST['email_address']); $City = mysql_real_escape_string($_REQUEST['city']); $State
I have a query like this SELECT TOWN, NAME FROM CINEMA WHERE CITY_ID =
I have this query to fetch the total OrderStatus that have values 1 and
In SQL Server, I have this query SELECT DISTINCT City FROM Person.Address But this
I have this in my view function: emit([doc.address.country,doc.address.state, doc.address.city], doc); When I query the
i have a query that goes like this: $get_feed_amount = mysql_query( SELECT COUNT(*) as
I have this query: @listings = Listing.where('city != ?', 'SommerVille') I was wondering if
I have this query: $queryf = SELECT * FROM classifieds WHERE city IN ('$cities')

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.