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

  • Home
  • SEARCH
  • 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 60225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:07:43+00:00 2026-05-10T18:07:43+00:00

Suppose you have two models, User and City, joined by a third model CityPermission:

  • 0

Suppose you have two models, User and City, joined by a third model CityPermission:

class CityPermission < ActiveRecord::Base   belongs_to :city   belongs_to :user end  class City < ActiveRecord::Base   has_many :city_permissions   has_many :users, :through => :city_permissions end  class User < ActiveRecord::Base   has_many :city_permissions   has_many :cities, :through => :city_permissions end 

Currently, I create the join table, and the index for the table, using the following migration code snippet:

create_table :city_permissions do |t|       t.integer :user_id, :city_id       t.other_fields ... end  add_index(:city_permissions, :user_id) add_index(:city_permissions, :city_id) 

Are these the optimal indexes to create? Will these indexes allow quick access back and forth through the join table, as well as quick lookups within the table itself, or is there some other better way? To restate this a bit differently, will these indexes, given city and user are instance variables of class City and User, allow city.users, city.city_permissions, user.cities, and user.city_permissions to all perform equally well?

  • 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. 2026-05-10T18:07:43+00:00Added an answer on May 10, 2026 at 6:07 pm

    Looks good to me.

    The joins generated should just be on either the PK IDs of the entity tables, or on the FK IDs in the join table – which are both indexes.

    Probably would be good to look at the generated ActiveRecord SQL and compare it against the indexes.

    Depending on what database you’re on you could then run that SQL through an Explain plan (or whatever tool exists, I’m thinking Oracle here)

    To simplify your code, you could look at using has_and_belongs_to_many as well. That would let you get rid of the CityPermission object (unless you want to use that to store data in itself)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have two classed Base and Derived , i.e.: #include <iostream> class Base
Suppose I have two applications written in C#. The first is a third party
Suppose I have two models, A and B , where an A can have
Suppose I have two classes with the same interface: interface ISomeInterface { int foo{get;
Suppose you have two seperate ASP.NET Web Application projects that both need to use
Suppose I have two branches of a project IMClient-MacOS and IMClient-Windows, and their code
Suppose that you have two huge files (several GB) that you want to concatenate
Suppose I have the following two strings containing regular expressions. How do I coalesce
Suppose I have a database table with two fields, foo and bar. Neither of
Suppose I have a tags table with two columns: tagid and contentid . Each

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.