LoginSignup
54
32

More than 5 years have passed since last update.

Rails の Active Record に like がない理由

Last updated at Posted at 2016-04-21

Active Record に like がない理由を見つけたのでメモっとく。

Rails 4 で not が追加されたとき likenot_like も追加される予定だった。実際のところコミットされた後にロールバックされた。

そのロールバックに対してのコメントが紛糾したわけだが、DHH がロールバックした理由を4つあげている。

Here are the design considerations that I had when we decided to roll this back:

1) It's an odd mismatch of SQL and Ruby with the % sign for designating the order.
2) It opens the door for wanting to expand this line of DSL further (including undesireable ventures into greater_or_equal_than).
3) It's being a fairly uncommon query not in need of optimization.
4) not_like is nasty (and "dislike" wont cut it either).

Feel free to extract this into a plugin and mix it into your own flavor of Rails, though.

拙いながら意訳してみる。(もっといい訳をコメントで提案して貰えたらと思う。)

我々がロールバックを決定したデザイン上の理由:

1) % 記号について SQL と Ruby で奇妙なミスマッチがある
2) DSL の境界線を押し広げる要求への扉を開いてしまう(greater_or_equal_than への望ましくない冒険を含む)
3) 最適化の必要がないとしてもかなり珍しいクエリである(like は Rails にて特殊な文法を新たに作ってサポートするほどにメジャーなクエリではない」という意味合い。sugitak さんのコメントより)
4) not_like はイケてない(dislike でもダメだ)

とはいうものの、遠慮なくこれをプラグインとして抜き出して、それを Rails の自前の味付けとして追加してもらって構わない。

僕としては likenot_like が入らなかったことを残念に思う気持ちもあるが、こういう判断が積み重なって一貫性のある設計の Rails が出来上がっていると考えると割と素直に受け入れられる。DHH の2番目の理由が一番納得できるかな。

LIKE を SQL でベタに書かなきゃならないとか信じられないって言ってる Rails エンジニアさんが近くにいたらそっとこのことを教えてあげてほしい。

54
32
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
54
32