Has_many to has_many ... do I need to add add_index in both directions?

I am new to Rails. I am wondering if I need add_index for both migrations?

I am trying to identify users and events. Each user can have many events, and each event can have many users. so I would do something like this:

class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation

  has_many :events
.
.
.

and then this:

class Event < ActiveRecord::Base
  attr_accessible :name

  has_many :users
.
.
.

I need to add add_index for both:

add_index :events, :user_id

and then

add_index :users, :event_id

Are these assumptions correct?

+3
source share
2 answers

Even without "indexes" your code will work, but as a best practice, it is useful to use "indexes" that will make your queries faster

check here

http://rails-bestpractices.com/posts/21-always-add-db-index

NTN

Sameera

+4
source

Railscast 's pretty cool many-to-many relationship.

+1
source

All Articles