Is there a performance gain in comparing objects like this...
current_user.id == @user.id
versus this ...
current_user == @user
Also are there best-practice reasons for doing one over the other regardless of performance?
Yes, but barely. ActiveRecord::Base#== does this:
def ==(comparison_object)
  super ||
    comparison_object.instance_of?(self.class) &&
    id.present? &&
    comparison_object.id == id
end
Which essentially compares ids but ensures that the objects are of the same type, which you probably want since, for example, if you compared just ids they could be the same even though one is a User and another a Product.
In conclusion, comparing the model objects themselves is best.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With