1

我想创建以下模型:

  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

如何添加一个多列索引,该索引仅索引翻译 = true (Postgres)。我想它看起来有点像这样:

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

更新

我正在使用 Rails 4。上面的语法是否正确?

更新 2

我将语法更改为

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

有关 Edgars Jekabsons 答案的更多信息,请访问https://coderwall.com/p/9hxejg

4

1 回答 1

2

我理解你的痛苦,香草 AR 3.2 版仍然不支持它。我在几个允许这样做的项目中使用了 pg_power gem。我强烈建议检查一下: https ://github.com/TMXCredit/pg_power

但是你应该可以在 Rails 4 中开箱即用。

于 2013-10-13T11:06:22.857 回答