-
Notifications
You must be signed in to change notification settings - Fork 21.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate plural association names on singular associations #45163
Deprecate plural association names on singular associations #45163
Conversation
c4a77f7
to
522502f
Compare
Nice find. Looking at the commit that introduced this, it seems like an accidental feature. Honestly I'm not even sure whether this is worth a deprecation cycle, as it never was actually a feature, it just happened to work. |
Yeah, I tested this and we never hit the deprecation warning in our test suite. I am up for skipping the deprecation cycle as well unless anyone feels strongly. |
I also tested this branch on our app, we do hit this deprecation a bit but it's clearly a mistake and will be very easy to fix on our side. So I really think we should not bother with a deprecation. |
At best if we want to make the error easier to fix, we could look into a "did_you_mean" style of error message. |
7016773
to
d6bdece
Compare
@byroot Updated with a |
Ah right, I was hopping we wouldn't but I now see how it's tricky. What about introducing an Active Record setting to disable this check, and have it default to disabled on 7.1 ? |
Actually thinking again about it. We really shouldn't bother. Let's just strip it now. |
d6bdece
to
4b5ba40
Compare
Before this change associations could be called by their plural or singular name, i.e. class Post < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :post end # this works with `post` as the key: Comment.where(post: Post.first) # this also works with `:posts` as the key (after this change this will warn): Comment.where(posts: Post.first) This PR deprecates using the plural association name when the association was not defined as plural. Motivated by rails#45020 which found that singularize call is slow and happens very often when you are querying a column name which is not a relation, so it must try both paths. After this deprecation cycle we can save that time in the `where(column_name: "value")` case. After this deprecation cycle, we can also avoid a potentially annoying edge case where you are unable to query a column with a singular name the same as your association's name. It seems this singularize was added 2 years ago here: rails#39465 , however inheritance and calculation tests still with this change removed.
4b5ba40
to
92eeaff
Compare
I love deleting code! Updated. Yeah, later we check if the |
This removes the singularize from `where` which runs on all `expand_from_hash` keys which might be reflections or column names. This saves a lot of time by avoiding singularizing column names. Previously in rails#45163 the singularize was removed entirely. after some reflection, I think it is better to at least give a warning for one release since `where` is a very popular API and the problems you can run into with incorrect relation could be hard to debug. Configurable with `ActiveRecord::Base.allow_deprecated_singular_assocaitions_name = false` / `config.active_record.allow_deprecated_singular_assocaitions_name = false`
…me [skip ci] The only time rails#45163 and rails#45344 have an effect is when the hash value passed to `where` is a model object. The current sample code does not change behavior between Rails 7.0 and 7.1
Before this change associations could be called by their plural or singular name,
i.e.
This PR deprecates using the plural association name when the association was not defined as plural.
Motivated by #45020 which found that singularize call is slow and happens very often when you are querying a column name which is not a relation, so it must try both paths. After this deprecation cycle we can save that time in the
where(column_name: "value")
case andwhere(association: "value")
case.After this deprecation cycle, we can also avoid a potentially annoying edge case where you are unable to query a column with a singular name the same as your association's name.
It seems this singularize was added 2 years ago here: #39465 , however inheritance and calculation tests still pass with this change removed. I also added a test for the enum case that is referenced in the the issue associated with the above PR to be extra safe.
for extra clarity, here is a test case that will pass after this deprecation that doesn't today,