考虑到以下关系:
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
我将如何最有效地添加索引来加速 Style 模型中的这种方法:
def has_feature? (arg)
self.features.where(:name=>arg).exists?
end