migu
migu

Reputation: 4336

Rails: Multicolumn partial index with constraint on one column

I want to create the following model:

  create_table "item_groups", force: true do |t|
    t.string   "name",                         null: false
    t.integer  "locale_id",                    null: false
    t.boolean  "translated",   default: false, null: false
  end

How do I add a multi column index that only indexes where translated = true (Postgres). I guess it would look a bit like this:

add_index "item_groups", ["translated", "locale_id"], name: "item_groups_translated_locale_id", where: "translated = true", using: :btree

Update

I'm using Rails 4. Is the syntax above correct?

Update 2

I changed the syntax to

add_index "item_groups", ["translated", "locale_id"], name: "item_groups_translated_locale_id", where: "(translated IS TRUE)", using: :btree

More info on Edgars Jekabsons' answer below and on https://coderwall.com/p/9hxejg

Upvotes: 1

Views: 612

Answers (1)

Edgars Jekabsons
Edgars Jekabsons

Reputation: 2853

I understand Your pain, it's still not supported in vanilla AR version 3.2. I used pg_power gem for several projects that allows to do it. I strongly encourage to check it out: https://github.com/TMXCredit/pg_power

But You should be able to do it in Rails 4 out of the box.

Upvotes: 2

Related Questions