0

I’m using Rails 4.2.7 and PostGres 9.5. I’m trying to create a trqm index on my table (to facilitate LIKE searches) so I created this migration

class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
  def change
    CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);
  end
end

This results in the below error when I run “rake db:migrate” …

StandardError: An error has occurred, this and all later migrations canceled:

undefined local variable or method `gin_trgm_ops' for #<AddTrqmIndexOnRaceTimes:0x007fdc34b40600>
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:664:in `block in method_missing'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/mikeb/Documents/workspace/runtrax/db/migrate/20161012185951_add_trqm_index_on_my_object_times.rb:3:in `change'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:608:in `exec_migration'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:590:in `migrate'

What’s the right way to create this index in a Rails migration?

3 Answers3

0

You cannot add SQL directly in the ruby method change. Either use the add_index method, or if you need to go beyond the capabilities of the helper, use ActiveRecord to execute your query like this:

class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
  def change
    MyObjectTime.connection.execute("CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);")
  end
end
Ariejan
  • 10,910
  • 6
  • 43
  • 40
  • How do I use the "add_index" method to execute the type of index I have specified in my question? –  Oct 13 '16 at 16:10
0

Rails 5 adds support for specifying operator classes on expression indexes

for example:

def change
  add_index :patients, 'lower(last_name) varchar_pattern_ops', name: "index_patients_on_name_unique", unique: true
end

source

Saiqul Haq
  • 2,287
  • 16
  • 18
0

ou need to make slight changes to your code. Wrap the sql query in execute.

class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
  def change
    execute "CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);"
  end
end
osazemeu
  • 87
  • 2
  • 11