Skip to content

Remove the magic :chain parameter from Relation#where#39784

Merged
rafaelfranca merged 1 commit intorails:masterfrom
marcrohloff:remove-magic-parameter-from-where
Oct 9, 2020
Merged

Remove the magic :chain parameter from Relation#where#39784
rafaelfranca merged 1 commit intorails:masterfrom
marcrohloff:remove-magic-parameter-from-where

Conversation

@marcrohloff
Copy link
Contributor

Summary

Removes the magic :chain parameter from Relation#where.
It appears to be unnecessary as this method is either called with no parameters (as in where.not) or with valid parameters.
No functionality is changed

Motivation

Magic parameters are bad. In this case it makes it hard to extend the method as there is no guarantee that :chain would never be a valid argument. Say I wanted to write an extension with syntax where(:title).like('%rails%') - that would break as soon as someone had a database field called :chain

Other Information

Although I prefer avoiding magic parameters where unnecessary, the alternative is to use the object constant pattern used elsewhere in Rails. Something like

  CHAIN = Object.new
  private_constant :CHAIN
  
   def where(opts = CHAIN, *rest)
      if CHAIN == opts
     <etc>  

though, IMO, this makes the method harder to extend than simply not having it at all

(An alternative would be to use a constant like `Enumerable#index_with`)
@rails-bot
Copy link

rails-bot bot commented Oct 2, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Oct 2, 2020
@rails-bot rails-bot bot closed this Oct 9, 2020
@rafaelfranca rafaelfranca reopened this Oct 9, 2020
@rafaelfranca rafaelfranca merged commit 2b1a400 into rails:master Oct 9, 2020
@eugeneius eugeneius removed the stale label Nov 15, 2020
@dhh
Copy link
Member

dhh commented Nov 24, 2020

This broke basic chaining:

>> Account.first.contacts.where.not(email_address: "1")
  Account Load (1.9ms)  SELECT `accounts`.* FROM `accounts` ORDER BY `accounts`.`id` ASC LIMIT 1
Traceback (most recent call last):
        2: from (irb):4
        1: from (irb):5:in `rescue in irb_binding'
ArgumentError (Unsupported argument type: chain (Symbol))

Going to revert.

@dhh
Copy link
Member

dhh commented Nov 24, 2020

Egg on my face. We've been developing an encryption extension to Active Record that's enroute for upstreaming. But it overwrote the old where signature, which caused the failure. I've reverted the revert. Sorry for the drama!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants