Abram
Abram

Reputation: 41884

Adding index in Rails has-many-through relationship

With the below relationship in mind:

class Style < ActiveRecord::Base
  has_many :stylefeatures, :dependent => :destroy
  has_many :features, :through => :stylefeatures
end

class Stylefeature < ActiveRecord::Base
  belongs_to :style
  belongs_to :feature
end

class Feature < ActiveRecord::Base
  has_many :stylefeatures, :dependent => :destroy
  has_many :styles, :through => :stylefeatures
end

How would I most efficiently add indexes to speed up this method in the Style model:

  def has_feature? (arg)
    self.features.where(:name=>arg).exists?
  end

Upvotes: 5

Views: 4998

Answers (3)

Tilo
Tilo

Reputation: 33732

class AddIndexesToStyleFeatures < ActiveRecord::Migration
  def self.up
    add_index :stylefeatures , [:style_id , :feature_id] , :unique => true
    add_index :features , :name    # check your data before making this unique
  end

  def self.down
    drop_index :features , :name
    drop_index :stylefeatures, [:style_id , :feature_id]
  end
end

You might want to make the :name index on the :features class unique, but beware of this catch:

If you have records which can contain NULL / nil fields which are part of the index, then don't use unique indexes. => check your data first

If during deletion of features it could happen that a StyleFeatures entry gets a nil reference (instead of being deleted altogether), then having a unique index will also cause problems for that table.

Make sure to double-check on how your particular database handles indexes when querying on null values.

See: Rails uniqueness constraint and matching db unique index for null column

and: How to create a unique index on a NULL column?

Upvotes: 8

Martin Fenner
Martin Fenner

Reputation: 76

Small change to answer by Tilo: use remove_index instead of drop_index:

class AddIndexesToStyleFeatures < ActiveRecord::Migration
  def self.up
    add_index :stylefeatures , [:style_id , :feature_id] , :unique => true
  end

  def self.down
    remove_index :stylefeatures, [:style_id , :feature_id]
  end
end

Upvotes: 1

Almaron
Almaron

Reputation: 4147

I'd recomend adding a unique index on stylefeatures style_id and feature_id (as an array) and a unique index on features.name.

Upvotes: 2

Related Questions