Monday, June 05, 2017

Tagging Multiple Models in Rails Notes

```ruby
class TagMultipleModels < ActiveRecord::Migration
  def change
    rename_column :taggings, :episode_id, :taggable_id
    add_column :taggings, :taggable_type, :string, :default => 'Episode'
    remove_foreign_key :taggings, :episodes
  end
end
```

rake db:rollback

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

Add a new migration for:     remove_foreign_key :taggings, :episodes

rails g migration remove_foreign_key_for_taggings


ActiveRecord::InvalidForeignKey


Delete teh new migration and create a new article instead of editing an existing record in an invalid state.


SELECT column FROM table WHERE column NOT IN
(SELECT intended_foreign_key FROM another_table)

select taggable_type from taggings where taggable_type NOT IN (select taggable_id from episodes);

Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails


Mysql2::Error: Cannot add or update a child row: a foreign key constraint fails (`chico_development`.`taggings`, CONSTRAINT `fk_rails_18a01188f6` FOREIGN KEY (`taggable_id`) REFERENCES `episodes` (`id`)): INSERT INTO `taggings` (`taggable_type`, `tag_id`, `taggable_id`, `created_at`, `updated_at`) VALUES ('Article', 542, 4752, '2017-06-06 03:14:34', '2017-06-06 03:14:34')


Import data from production. Merge the last two new migrations to one migration:


brew install elasticsearch

==> Using the sandbox
==> Downloading https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz
######################################################################## 100.0%
==> Caveats
Data:    /usr/local/var/elasticsearch/elasticsearch_bparanj/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_bparanj.log
Plugins: /usr/local/opt/elasticsearch/libexec/plugins/
Config:  /usr/local/etc/elasticsearch/
plugin script: /usr/local/opt/elasticsearch/libexec/bin/elasticsearch-plugin

To have launchd start elasticsearch now and restart at login:
  brew services start elasticsearch
Or, if you don't want/need a background service you can just run:
  elasticsearch
==> Summary
  /usr/local/Cellar/elasticsearch/5.4.1: 100 files, 35.4MB, built in 1 minute 7 seconds


elasticsearch

org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: failed to obtain node locks, tried [[/usr/local/var/elasticsearch/elasticsearch_bparanj]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])?


$ brew remove elasticsearch
$ sudo rm -rf /usr/local/var/elasticsearch
$ sudo rm -rf /usr/local/etc/elasticsearch

$ brew install elasticsearch

brew install elasticsearch
==> Using the sandbox
==> Downloading https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz
Already downloaded: /Users/bparanj/Library/Caches/Homebrew/elasticsearch-5.4.1.tar.gz
==> Caveats
Data:    /usr/local/var/elasticsearch/elasticsearch_bparanj/
Logs:    /usr/local/var/log/elasticsearch/elasticsearch_bparanj.log
Plugins: /usr/local/opt/elasticsearch/libexec/plugins/
Config:  /usr/local/etc/elasticsearch/
plugin script: /usr/local/opt/elasticsearch/libexec/bin/elasticsearch-plugin

To have launchd start elasticsearch now and restart at login:
  brew services start elasticsearch
Or, if you don't want/need a background service you can just run:
  elasticsearch
==> Summary
/usr/local/Cellar/elasticsearch/5.4.1: 100 files, 35.4MB, built in 12 seconds
$elasticsearch

  Article Store (14.7ms)  {"id":5091,"exception":["Faraday::ConnectionFailed","Couldn't connect to server"]}
Completed 500 Internal Server Error in 50ms (Searchkick: 14.7ms | ActiveRecord: 9.6ms)


Rails.cache.clear if new production import does not show new episodes.