squeel icon indicating copy to clipboard operation
squeel copied to clipboard

Delegate to `visit` instead of calling `.id` directly

Open bollard opened this issue 9 years ago • 4 comments

This is related to #348

Translation of an ActiveRecord::Base object to an integer should be defined once (i.e. in visit_ActiveRecord_base). This makes it easier for the case when a user does not want to query by id but some other field, for example persistent_id

bollard avatar Oct 30 '14 14:10 bollard

:-1: on this. AR maps Base#id to whatever the primary key is.

ernie avatar Oct 30 '14 14:10 ernie

And AR can still can continue to map Base#id to whatever it wants...

  def visit_ActiveRecord_Base(o, parent)
    if some_condition
      o.id # Call Base#id and retrieve primary key
    else
      # some thing else
    end
  end

But at the Squeel level I would hope this could be configurable.

Squeel is a useful abstraction away from Arel / AR which has allowed us to monkey patch bi-temporal support into Rails (for which this PR helps me continue to use). I very much hope this level of abstraction can be maintained to help support plugin developers

bollard avatar Oct 30 '14 14:10 bollard

Monkey patching Squeel which monkey patches ActiveRecord which monkey patches everything hardly seems like a sustainable path forward for your app/library.

ernie avatar Oct 30 '14 15:10 ernie

I agree it is certainly not an ideal situation to be in, however when making such invasive changes to Rails we were always going to have to patch a number of places in the internals.

This proposed patch is quite the opposite though - a small innocuous change which will have no noticeable impact to almost all of your users, but be incredibly useful to a small number of them and arguably make the internal API more consistent :smile:

bollard avatar Oct 30 '14 16:10 bollard