What indexes should be added for a polymorphic association in Ruby on Rails?

I have a polymorphic association in a Ruby on Rails model. In the migration, I have:

create_table "offer_defs" do |t|
  t.integer "product_def_id"
  t.string  "product_def_type"

I would like to add an index for this association. I am hesitating between the following options:

add_index :offer_defs, [:product_def_id, :product_def_type]


add_index :offer_defs, :product_def_id
add_index :offer_defs, :product_def_type

or maybe both ?

What are the pros and cons?


I would go with the latter option. It'll help if you need to count polymorphic attachments of a specific type, which you might. If you wanted the multi column index, I'd at least swap the order of the two columns (putting type first), since that type field is much more likely to be a query param on this table than the id field.

note: I'm assuming you're using mysql here, and my advice should probably be ignored, or at least checked, if you're using a different db.

Need Your Help

javascript: receive radio button value in a radio group and command different instruction according to each case

java javascript html jsp

I want to store new row in a database table when a person checked a first radio button in a radio group. Inserting database row is ok. But I don't know a below part among the whole codes.

how can i make a video/image cube interactive?

3d cinema-4d lingo adobe-director

im doing a project in adobe director and im looking to make an image/video cube which display an image or video on each side of the cube now i have seen some tutorial on how to make the cube in sof...