Hard work by INTERNET

ベンチャーで働くひとりぼっちWEB開発者が頑張るブログ

Should AR where support `db_name.table_name.column_name` to key of hash?

issueの本文開始。

ActiveRecordのwhereは ハッシュのキー「 db_name.table_name.column_name」をサポートするべきです。

Now db_name.table_name.column_name is not supported as key of hash

今、db_name.table_name.column_nameはハッシューのキーとしてサポートしていない。

Rails 4.2.3 with mysql2

[1] pry(main)> User.where("id" => 1).to_sql => "SELECT users.* FROM users WHERE users.id = 1"

[2] pry(main)> User.where("users.id" => 1).to_sql => "SELECT users.* FROM users WHERE users.id = 1"

[3] pry(main)> User.where("db.users.id" => 1).to_sql => "SELECT users.* FROM users WHERE db.users.id = 1"

[3] is invalid SQL because db.users.id

Rails 5.0.0 (07d6e1dc1fc78abaf46f9a73ded0b370ba489f6c) with mysql2

[1] pry(main)> User.where("id" => 1).to_sql => "SELECT users.* FROM users WHERE users.id = 1"

[2] pry(main)> User.where("users.id" => 1).to_sql => "SELECT users.* FROM users WHERE users.id = 1"

[3] pry(main)> User.where("db.users.id" => 1).to_sql => "SELECT users.* FROM users WHERE db.users = 1"

!!!! column name is lost!

Sure when we add database_name as AR.table_name these method create valid SQL

ActiveRecord.table_nameとしてdatabaseを追加すると正しいSQLを作ること確かめますね。

class User < ActiveRecord::Base self.table_name = "rails_spike_development.users" end

Rails 4.2.3 with mysql2

[1] pry(main)> User.where("id" => 1).to_sql => "SELECT rails_spike_development.users.* FROM rails_spike_development.users WHERE rails_spike_development.users.id = 1"

Rails 5.0.0 (07d6e1dc1fc78abaf46f9a73ded0b370ba489f6c) with mysql2

[1] pry(main)> User.where("id" => 1).to_sql => "SELECT rails_spike_development.users.* FROM rails_spike_development.users WHERE rails_spike_development.users.id = 1"

Both of them work correctly

But some gems use table_name as key

しかし、いくつかのgemはkeyとしてtable_nameを使う。

https://github.com/mbleigh/acts-as-taggable-on/blob/master/lib/acts_as_taggable_on/taggable/core.rb#L319 https://github.com/aasm/aasm/blob/master/lib/aasm/persistence/base.rb#L61

Should AR where support db_name.table_name.column_name to key of hash?

AR.whereは ハッシュのキーとしてdb_name.table_name.column_name をサポートするべきではありませんか?

レスの1つ目

@yui-knk did you find the commit that changed how this works? Maybe there's some insight there why this change happened in the first place.

あなたは、この働きに変更したコミットを見つけましたか?まー僕は発端はいくつか思い当たるけど。

レス2つ目

@senny I found out this commit change behavior.

私は変更の振る舞いのコミットを見つけた。 [...]

レス3つ目

We should definitely continue to make sure that this continues to generate invalid SQL and not silently do the wrong thing. That part should be easy to implement.

In terms of actually supporting this, I'm not a fan of allowing the db name in the string (or the dot notation in general for calls to where). One thing I am hoping to do is have where more specifically target the association, rather than the table, in order to gain more information about the model we're joining on. Once that lands, we could allow specifying database_name on the model class. However, I'd want to see a concrete use case for that, since if you statically know the database that the table lives on, I'm not sure why you'd want it on separate DBs to begin with.

The sharding use case is more complex (too much for me to get in depth on right now), but it is something I want to tackle (not so much to say "this is how you shard in Rails", but more to add the hooks required for you to do it without monkey patching). It's something I'll be working on in the near future.

私たちは、誤ったSQLを作成し、静かに誤りは続けずに間違いなく確認し続けるべき。 [...]

このdb_nameをハッシュのキーに入れたいということは了解してくれたみたいだけど、いつ取り込まれるんだろう。 目が離せません。

Should AR where support `db_name.table_name.column_name` to key of hash? · Issue #21326 · rails/rails · GitHub Remove @klass.table_name from scope conditions key by yui-knk · Pull Request #244 · aasm/aasm · GitHub