とりあえずユーザー管理と認証を実装したいのでdeviseを使う

自力で実装する心の余裕が消えたのでdeviseを使うことにしました。
もうすでにあるものはガンガン使おう!そうしよう。

インストールや設定手順は下記リンクを参照しました。

手順

Gemfileにdeviseを追加する。

gem 'devise'

bundle installを行う。
(ここではbundlerの設定をそのまま使用したいのでbundleコマンドを実行している)

$ bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.5.0
・・・略・・・
Using rack-test 0.6.3
Installing orm_adapter 0.5.0
Installing warden 1.2.6
Installing bcrypt 3.1.11 with native extensions
Using mail 2.6.3
・・・略・・・
Using web-console 2.3.0
Installing responders 2.1.1
Installing devise 3.5.6
Bundle complete! 16 Gemfile dependencies, 72 gems now installed.
Bundled gems are installed into ./vendor/bundle.

devise関連ファイルのをrailsコマンドで生成する。

$ bundle exec rails generate devise:install
      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================

Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

       config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

     In production, :host should be set to the actual host of your application.

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>

  4. If you are deploying on Heroku with Rails 3.2 only, you may want to set:

       config.assets.initialize_on_precompile = false

     On config/application.rb forcing your application to not access the DB
     or load models when precompiling your assets.

  5. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

===============================================================================

環境を設定する。
config/environments/development.rb に下記を追記する。

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

devise用のUserモデルを作成する。

$ bundle exec rails g devise User
      invoke  active_record
      create    db/migrate/20160321071935_devise_create_users.rb
      create    app/models/user.rb
      invoke    rspec
      create      spec/models/user_spec.rb
      insert    app/models/user.rb
       route  devise_for :users

Userテーブルを追加するのでmigrateする。

$ bundle exec rake db:migrate:reset
== 20160321071935 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0166s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0333s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0259s
== 20160321071935 DeviseCreateUsers: migrated (0.0760s) =======================

deviseが出力するメッセージの表示場所を設定する。
(ここでは app/views/layouts/application.html.erb に設定)
ついでにナビゲーションバーにログインしていない場合はログイン、ログイン済みの場合はログアウトというリンクを表示する。

<body>
  <header>
    <div class="">
      <% if user_signed_in? %>
        <div>
          Logged in as <strong><%= current_user.email %></strong>.
          <%= link_to "ログアウト", destroy_user_session_path, method: :delete %>
        </div>
      <% else %>
        <div>
          <%= link_to "ログイン", new_user_session_path %>
        </div>
      <% end %>

    </div>
  </header>

  <div>
    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>
    <%= yield %>
  </div>

</body>

undefined method `devise_for'というエラーが出た。
f:id:maetoo11:20160321175102p:plain
→一度rails serverを再起動しないといけないらしい。
undefined method `devise_for' in rails - Stack Overflow

正しく動けば以下のようなログイン画面が表示されるはず。
f:id:maetoo11:20160321175236p:plain

その他

ログインしていないユーザーはログイン画面にリダイレクトするよう設定したかったので下記を参考に実装。
devise + omniauth-facebookでリクエストURLにリダイレクトする - yasuoza diary

初期ユーザーの追加をする。
Devise に初期ユーザを追加 - Ruby and Rails


Ruby on Rails 4 アプリケーションプログラミング

Ruby on Rails 4 アプリケーションプログラミング

N+1問題を検出してくれるgem、bulletを導入する

Railsで作ったプロジェクトを開発環境で動かしていくと、ある一覧ページを表示する度にすごい量のSELECTが発行されているように見え、先輩に相談!

「N+1問題」というものを教えていただきました。
N+1問題 / Eager Loading とは - Rails Webook

解決策

以下のページを参考にしました。
ActiveRecordのjoinsとpreloadとincludesとeager_loadの違い - Qiita

そもそもN+1問題はログを眺めていないと気づけないのでは…?と思ったところ、素敵なgemがあること先輩に教えていただきました!
それが「bullet」です。
GitHub - flyerhzm/bullet: help to kill N+1 queries and unused eager loading

導入手順

導入は下記ページを参考にしました。
» Railsライブラリ紹介: N+1問題を検出する「bullet」 TECHSCORE BLOG

Gemfileにbulletを追加
(開発環境でのみ使用する)

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
  gem 'thin'
  gem 'rails_best_practices'
  gem 'bullet'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

bundleコマンドを実行し、gemをインストールする

$ bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.5.0
Using i18n 0.7.0
・・・略・・・
Using sass-rails 5.0.4
Using web-console 2.3.0
Installing uniform_notifier 1.9.0
Installing bullet 5.0.0
Bundle complete! 15 Gemfile dependencies, 67 gems now installed.
Bundled gems are installed into ./vendor/bundle.

開発環境のconfigにbullet用の設定を追加する
(config/environments/development.rbというファイルに下記の記述を追加する)

  config.after_initialize do
    Bullet.enable = true
    Bullet.alert = true
    Bullet.bullet_logger = true
    Bullet.console = true
    Bullet.rails_logger = true
  end

railsサーバーを再起動する
(再起動しないとbulletが使えない)

再起動してN+1問題のクエリが発行されるページへアクセスすると、下記のようなポップアップが表示されるようになる

f:id:maetoo11:20160314212116p:plain:w500

どこの処理で発生したかに加え対処方法をきちんと教えてくれるので、対処が簡単になりました(∩´∀`)∩ワーイ

素敵なgem、rails_best_practicesを導入する

先輩のアドバイスを受け、Railsのベストプラクティスに従っているかを確認できるgemを導入しました。

手順

Gemfileにrails_best_practicesを追加(今回は開発環境だけで使用する)

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
  gem 'thin'
  gem 'rails_best_practices'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
end

bundleコマンドを実行し、gemをインストールする

$ cat .bundle/config
---
BUNDLE_PATH: vendor/bundle
BUNDLE_JOBS: 4
BUNDLE_DISABLE_SHARED_GEMS: '1'
$
$
$ bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using json 1.8.3
Using minitest 5.8.4
・・・略・・・
Installing code_analyzer 0.4.5
Installing rails_best_practices 1.16.0
Bundle complete! 13 Gemfile dependencies, 64 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Post-install message from rails_best_practices:
********************************************************************************

  rails_best_practices is a code metric tool to check the quality of rails codes.

  I highly recommend you browse the Rails Best Practices website first.

      http://rails-bestpractices.com

  Please also try our online service

      http://railsbp.com

  Enjoy!

      Richard Huang (flyerhzm@gmail.com)

********************************************************************************
$

これでrails_best_practicesが使える状態になる。

早速実行してみる!
プロジェクトのディレクトリに入り、 rails_best_practices . を実行する。

$ bundle exec rails_best_practices .
Source Code: |==============================================================================================================================|
/Users/maetoo11/workspace/rails-app-name/db/schema.rb:34 - always add db index (model_name => [column_name])
/Users/maetoo11/workspace/rails-app-name/app/views/hoge/index.html.erb:53 - law of demeter
/Users/maetoo11/workspace/rails-app-name/app/helpers/humus_helper.rb:1 - remove empty helpers
/Users/maetoo11/workspace/rails-app-name/app/views/humus/_form.html.erb:1 - replace instance variable with local variable
/Users/maetoo11/workspace/rails-app-name/app/views/hoge/_form.html.erb:7 - replace instance variable with local variable

Please go to http://rails-bestpractices.com to see more useful Rails Best Practices.

Found 28 warnings.

各警告メッセージの意味(日本語)は下記の記事を参考にしました。
Rails Best Practices の警告をちゃんと考える - Qiita

Rails Best Practicesの本家ページ(?)です。
Rails Best Practices - Rails Best Practices
※どうやってプラクティスを検索していいかが不明…。

これでベストプラクティスをできるだけ守ったコードがかけるはず!!

rails newの先輩オススメ手順

railsプロジェクトを新しく作成する際に実行する「rails new」のオススメ手順を、会社の先輩に教えていただいたのでメモ。

前提

  • Rubyインストール済み
  • アプリケーション名:rails-app-name

手順

railsアプリケーション用のディレクトリを作成する。
→作成したディレクトリに移動

$ mkdir rails-app-name
$
$ cd rails-app-name
$
$ pwd
/Users/maetoo11/workspace/rails-app-name
$
$ ls -l
$

railsアプリケーション用ディレクトリでbundle initを実行し、Gemfileを作成する。

$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
$
$ bundle init
Writing new Gemfile to /Users/maetoo11/workspace/rails-app-name/Gemfile
$
$
$
$ ls -l
total 8
-rw-r--r--  1 maetoo11  staff  64  3  3 17:37 Gemfile

Gemfileを編集してrailsのgem を追加する。

$ vi Gemfile
  1 source 'https://rubygems.org'
  2
  3 gem 'rails'

bundle installでGemfileで指定されたgemのインストールを行う。

ここで大事なこと!

$ bundle install --path vendor/bundle --jobs=4
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using json 1.8.3
Installing i18n 0.7.0
Installing minitest 5.8.4
Installing rake 10.5.0
Installing builder 3.2.2
・・・略・・・
Installing rails 4.2.5.2
Bundle complete! 1 Gemfile dependency, 34 gems now installed.
Bundled gems are installed into ./vendor/bundle.
$
$ ls -l
total 16
-rw-r--r--  1 maetoo11  staff    63  3  3 17:37 Gemfile
-rw-r--r--  1 maetoo11  staff  2607  3  3 17:40 Gemfile.lock
drwxr-xr-x  3 maetoo11  staff   102  3  3 17:40 vendor
$
$ # vendor/bundle配下にgemがインストールされていることを確認
$ ls -la vendor/bundle/ruby/2.3.0/gems
total 0
drwxr-xr-x  34 maetoo11  staff  1156  3  3 17:40 .
drwxr-xr-x   9 maetoo11  staff   306  3  3 17:40 ..
drwxr-xr-x   6 maetoo11  staff   204  3  3 17:40 actionmailer-4.2.5.2
drwxr-xr-x   6 maetoo11  staff   204  3  3 17:40 actionpack-4.2.5.2
・・・略・・・

rails newを実行する際に、付与するオプションを決める。
→まずはヘルプを表示して、オプションを確認

$ bundle exec rails new -h
Usage:
  rails new APP_PATH [options]
Options:
  -r, [--ruby=PATH]                                      # Path to the Ruby binary of your choice
                                                         # Default: /Users/maetoo11/.rbenv/versions/2.3.0/bin/ruby
  -m, [--template=TEMPLATE]                              # Path to some application template (can be a filesystem path or URL)
      [--skip-gemfile], [--no-skip-gemfile]              # Don't create a Gemfile
  -B, [--skip-bundle], [--no-skip-bundle]                # Don't run bundle install
  -G, [--skip-git], [--no-skip-git]                      # Skip .gitignore file
      [--skip-keeps], [--no-skip-keeps]                  # Skip source control .keep files
  -O, [--skip-active-record], [--no-skip-active-record]  # Skip Active Record files
  -S, [--skip-sprockets], [--no-skip-sprockets]          # Skip Sprockets files
      [--skip-spring], [--no-skip-spring]                # Don't install Spring application preloader
  -d, [--database=DATABASE]                              # Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
                                                         # Default: sqlite3
  -j, [--javascript=JAVASCRIPT]                          # Preconfigure for selected JavaScript library
                                                         # Default: jquery
  -J, [--skip-javascript], [--no-skip-javascript]        # Skip JavaScript files
      [--dev], [--no-dev]                                # Setup the application with Gemfile pointing to your Rails checkout
      [--edge], [--no-edge]                              # Setup the application with Gemfile pointing to Rails repository
      [--skip-turbolinks], [--no-skip-turbolinks]        # Skip turbolinks gem
  -T, [--skip-test-unit], [--no-skip-test-unit]          # Skip Test::Unit files
      [--rc=RC]                                          # Path to file containing extra configuration options for rails command
      [--no-rc], [--no-no-rc]                            # Skip loading of extra configuration options from .railsrc file
Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist
Rails options:
  -h, [--help], [--no-help]        # Show this help message and quit
  -v, [--version], [--no-version]  # Show Rails version number and quit
Description:
    The 'rails new' command creates a new Rails application with a default
    directory structure and configuration at the path you specify.
    You can specify extra command-line arguments to be used every time
    'rails new' runs in the .railsrc configuration file in your home directory.
    Note that the arguments specified in the .railsrc file don't affect the
    defaults values shown above in this help message.
Example:
    rails new ~/Code/Ruby/weblog
    This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
    See the README in the newly created application to get going.

今回は

  • bundle installを飛ばす
  • DBにMySQLを指定
  • turbolinksを使用しない
  • test-unitを使用しない

を指定してrails newを実行する。

2016/12/16 追記
Rails5からは
bundle exec rails new -B -d mysql --skip-turbolinks --skip-test.
らしいです。(未確認です)

$ bundle exec rails new -B -d mysql --skip-turbolinks --skip-test-unit .
       exist
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
    conflict  Gemfile
Overwrite /Users/maetoo11/workspace/rails-app-name/Gemfile? (enter "h" for help) [Ynaqdh]
       force  Gemfile
      create  app
・・・略・・・
$
$ ls -l
total 40
-rw-r--r--   1 maetoo11  staff  1392  3  3 17:48 Gemfile
-rw-r--r--   1 maetoo11  staff  2607  3  3 17:40 Gemfile.lock
-rw-r--r--   1 maetoo11  staff   478  3  3 17:48 README.rdoc
-rw-r--r--   1 maetoo11  staff   249  3  3 17:48 Rakefile
drwxr-xr-x   8 maetoo11  staff   272  3  3 17:48 app
drwxr-xr-x   6 maetoo11  staff   204  3  3 17:48 bin
drwxr-xr-x  11 maetoo11  staff   374  3  3 17:48 config
-rw-r--r--   1 maetoo11  staff   153  3  3 17:48 config.ru
drwxr-xr-x   3 maetoo11  staff   102  3  3 17:48 db
drwxr-xr-x   4 maetoo11  staff   136  3  3 17:48 lib
drwxr-xr-x   3 maetoo11  staff   102  3  3 17:48 log
drwxr-xr-x   7 maetoo11  staff   238  3  3 17:48 public
drwxr-xr-x   3 maetoo11  staff   102  3  3 17:48 tmp
drwxr-xr-x   4 maetoo11  staff   136  3  3 17:48 vendor

Gemfileを編集し、不要なgemの削除と必要なgemの追加を行う。
→今回は開発・テスト環境でwebrickではなくthinを使うようにした。(39行目)

$ vi Gemfile
  1 source 'https://rubygems.org'
  2
  3
  4 # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
  5 gem 'rails', '4.2.5.2'
  6 # Use mysql as the database for Active Record
  7 gem 'mysql2', '>= 0.3.13', '< 0.5'
 ・・・略・・・
 35
 36 group :development do
 37   # Access an IRB console on exception pages or by using <%= console %> in views
 38   gem 'web-console', '~> 2.0'
 39   gem 'thin'
 40
 41   # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
 42   gem 'spring'
 43 end

vendor/bundle配下をgitで管理しないファイルにするために、.gitignoreを編集する。(9行目)

$ vi .gitignore
  1 # See https://help.github.com/articles/ignoring-files for more about ignoring files.
  2 #
  3 # If you find yourself ignoring temporary files generated by your text editor
  4 # or operating system, you probably want to add a global ignore instead:
  5 #   git config --global core.excludesfile '~/.gitignore_global'
  6
  7 # Ignore bundler config.
  8 /.bundle
  9 /vendor/bundle
 10
 11 # Ignore all logfiles and tempfiles.
 12 /log/*
 13 !/log/.keep
 14 /tmp

bundlerの設定を確認する。
→最初のbundle install時に指定したオプションが設定されている。
→以降、このプロジェクトディレクトリで実行するbundlerはこの設定を使用する。

$ cat .bundle/config
---
BUNDLE_PATH: vendor/bundle
BUNDLE_JOBS: 4
BUNDLE_DISABLE_SHARED_GEMS: '1'

bundle installを実行する。

$ bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.5.0
Using i18n 0.7.0
Using json 1.8.3
Using minitest 5.8.4
・・・略・・・
Bundle complete! 11 Gemfile dependencies, 53 gems now installed.
Bundled gems are installed into ./vendor/bundle.
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
 = 1.8.7 : gem install rdoc-data; rdoc-data --install
 = 1.9.1 : gem install rdoc-data; rdoc-data --install
>= 1.9.2 : nothing to do! Yay!

これでrails newのオススメ手順は終わりです。

新しいRailsプロジェクトを作成する

何も思い出せなかったので、自分のメモ用です。
こうしたほうがいいよって部分があれば教えていただきたいです。

前提

  • rbenvを使用している
  • 既存のRailsプロジェクトとは違うバージョンのRailsを使用する
  • Ruby2.3.0+Rails4.2.5を使用する
  • 作成するアプリ名は「hoge」とする

実際の作業

現在のRubyのバージョンを確認。

$ rbenv versions
  system
  2.1.2
* 2.1.5
  2.2.2

2.3.0がインストールできるバージョンの一覧にない。

$ rbenv install -l
Available versions:
  1.8.6-p383
  1.8.6-p420
  ・・・(略)・・・
  2.2.0
  2.2.1
  2.2.2
  2.2.3
  2.3.0-dev
  jruby-master+graal-dev
  jruby-1.5.6
  ・・・(略)・・・

ruby-buildをupdateする。

$ brew upgrade ruby-build
==> Upgrading 1 outdated package, with result:
ruby-build 20160130
==> Upgrading ruby-build
==> Installing dependencies for ruby-build: pkg-config, openssl
・・・(略)・・・
🍺  /usr/local/Cellar/ruby-build/20160130: 196 files, 110.1K, built in 4 seconds

2.3.0がインストールできるバージョンの一覧に出た!

$ rbenv install -l
Available versions:
  1.8.6-p383
  1.8.6-p420
  ・・・(略)・・・
  2.2.4
  2.3.0-dev
  2.3.0-preview1
  2.3.0-preview2
  2.3.0
  2.4.0-dev
  jruby-1.5.6
  jruby-1.6.3
  ・・・(略)・・・

Ruby 2.3.0をインストール

$ rbenv install 2.3.0
Downloading ruby-2.3.0.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.0.tar.bz2
Installing ruby-2.3.0...
Installed ruby-2.3.0 to /Users/****/.rbenv/versions/2.3.0

インストールされたことを確認

$ rbenv versions
  system
  2.1.2
* 2.1.5
  2.2.2
  2.3.0

使用するRubyのバージョンを2.3.0に切り替える

$ rbenv global
2.1.5
$ rbenv global 2.3.0
$
$ rbenv versions
  system
  2.1.2
  2.1.5
  2.2.2
* 2.3.0

Railsのgemをインストール(最新が欲しかったので、バージョン指定なし)

$ gem install rails --no-document
Fetching: railties-4.2.5.1.gem (100%)
Successfully installed railties-4.2.5.1
Fetching: rails-4.2.5.1.gem (100%)
Successfully installed rails-4.2.5.1
2 gems installed

--no-document については下記URLを参照。
gemのインストールで`--no-document`を付けると77倍早い - Qiita

カレントディレクトリにRailsアプリケーションを作成(READMEのみのリポジトリが作成されていたため、事前にgit cloneしておいた)

$ rails new . -d mysql
       exist  
      create  README.rdoc
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/assets/javascripts/application.js
      create  app/assets/stylesheets/application.css
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      ・・・(略)・・・
         run  bundle install
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies.....
Installing rake 10.5.0
・・・(略)・・・

起動してみる

$ rails s
=> Booting WEBrick
=> Rails 4.2.5.1 application starting in development on http://localhost:3000
・・・(略)・・・

ActiveRecord::NoDatabaseErrorになる。
→まだDBを作成してないから当たり前。

DBを作成する。

$ rake db:create
$ 
$ #databeseが作成されたことを、mysqlコンソールから確認する。
$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.27 Homebrew

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+----------------------------------------+
| Database                               |
+----------------------------------------+
| mysql                                  |
| hoge_development                       |
| hoge_test                              |
+----------------------------------------+

再度 rails s で起動し、http://localhost:3000/ にアクセスすると、下記のような画面が表示される。

f:id:maetoo11:20160223144247p:plain

アプリケーションの作成はここまで。

Geek Women New Years Party 2016 でLTをしました

Geek Women New Years Party 2016というイベントが先週の金曜日(1/15)に開催され、 イベントの中でLTをさせていただきました。

eventdots.jp

LT内容について

www.slideshare.net

私が働いている会社のCTOについてお話しました。
発表後に「CTO素敵ですね!」と声をかけていただいたのが嬉しかったです。
ほんとうに、うちのCTOは素敵なんですよ!!!

ペコッターについて

よくお声がけしていただくのですが、 私はペコッターの開発者ではありません。
ペコッターのキャラクター「はらぺこ君」が大好きすぎて、はらぺこエヴァンジェリストを勝手に名乗っています。

お菓子神社について

下記の記事を参考に、社内にオフィスグリコのようなお菓子置き場を作っています。
チーム開発 - 3年くらいお菓子神社運営してきた - Qiita
オフィスグリコよりも、もっと少量を気軽に食べられる状態をつくりたいと思って始めました。


女性がたくさん集まったキラキラしたイベントでした(・∀・)
女性のコミュニティ、色々あるんですねー。

重複したレコードのINSERTを防止する

前にやっていたブログから移行してきた記事です。

idとcreated_atだけが異なるレコードが重複して登録されることがあって困っていた。
同じレコードがないときはINSERT、同じレコードがある場合はUPDATEが実行されるようにしたい。
例として以下の様なテーブルを用いて説明する。

work_itemsテーブル

id created_at work item user
1 2015-08-25 14:59:00 1 ホチキス やまだ
2 2015-08-28 19:04:00 1 はさみ たなか
3 2015-08-28 19:35:00 2 はさみ やまだ
4 2015-08-28 21:15:00 2 はさみ たなか

※例のためのテーブルなので構成がテーブルとしておかしいです。

下記を参考に実装する。
MySQL で INSERT と UPDATE を1文で実現する ~ ON DUPLICATE KEY UPDATE 編~ | UB Lab.

MySQLのユニーク制約とは?
ユニークキー制約(UNIQUE) - テーブルの作成 - MySQLの使い方

今回はworkとitem両方が同じものは登録したくない。
→workとitemの2つでユニークになるようにしたい。

複数カラムにユニーク制約をつけたい!
ActiveRecord4のバリデーションで複数カラムにユニーク制約を付ける方法
Rails でユニーク制約 - @tmtms のメモ

元々存在するテーブルなので、ユニーク制約を追加する。

class AddUniqueToWorkItem < ActiveRecord::Migration
  def change
    add_index :work_items, [:work, :item], :unique=>true
  end
end


・・・ちょっとまった!!!
元々workとitemが重複しているレコードがあるのに後からユニーク制約つけようとしたらmigrateはどうなるんだ?!?!

実際にやってみた!

Mysql2::Error: Duplicate entry '21-3' for key 'index_chat_work_items_on_work_and_item': CREATE UNIQUE INDEX `index_chat_work_items_on_work_and_item`  ON `work_items` (`work`, `item`) /test/db/migrate/20150831000000_add_unique_to_work_items.rb:3:in `change'

わーい(∩´∀`)∩ワーイ
落ちたー!

『ALTER IGNORE』を使えば大丈夫そうだ!
複数カラムでユニークキーを作る - 揮発性のメモ

過去データが書き換わってしまう(重複していた場合は先にあるほうが残る)がOKであるかはサービスやプロジェクトによると思います!

class AddUniqueToWorkItem < ActiveRecord::Migration
  def up
    sql = "ALTER IGNORE TABLE work_item ADD UNIQUE duplicate_prevent (work, item);"
    ActiveRecord::Base.connection.execute(sql)
  end

  def down
    sql = "ALTER TABLE work_item DROP INDEX duplicate_prevent;"
    ActiveRecord::Base.connection.execute(sql)
  end
end


これであとはINSERTするときにON DUPLICATE KEY UPDATEすればOKだ♪

now = Time.now.strftime("%Y-%m-%d %H:%M:%S")
query = "INSERT INTO `work_items` (`created_at`, `work`, `item`, `user`) VALUES ('%s', %d, %s, %s) ON DUPLICATE KEY UPDATE created_at = `%s`"
query = query % [
  now,
  [workの値],
  [itemの値],
  [userの値],
  now
]
ActiveRecord::Base.connection.execute(query)


・・・と思ったら、パーティションロックがかかるらしいorz
MySQL :: MySQL 5.6 リファレンスマニュアル :: 13.2.5.3 INSERT ... ON DUPLICATE KEY UPDATE 構文

該当テーブルのINSERTでユニーク制約違反例外が発生したときに無視する、という方針に変更。

begin
  WorkItem.create(
    work:    [workの値],
    item:     [itemの値],
    user:     [userの値]
  )
rescue ActiveRecord::RecordNotUnique => exception
  # ユニーク制約違反は無視する
end

これで重複記録はなくなるはず。


メモ