Search code examples
ruby-on-railsrubyactiverecordbenchmarking

Ruby minus operator with ActiveRecord objects


Can you explain pls, how's Ruby's minus operator working? Not just trivial case like 5 - 2. A bit more complex – we have 2 arrays with ActiveRecord objects: Array A = User.where(...), Array B = User.where(...), I wanna make A - B, how is it working? Is it just comparing objects IDs, or all attributes, or smth else?


Solution

  • Is it just comparing objects IDs?

    Type and ID, yes.

    pry(main)> show-source User#eql?
    
    From: /Users/sergio/.gem/ruby/2.5.1/gems/activerecord-5.2.0/lib/active_record/core.rb @ line 420:
    Owner: ActiveRecord::Core
    Visibility: public
    Number of lines: 6
    
    def ==(comparison_object)
      super ||
        comparison_object.instance_of?(self.class) &&
        !id.nil? &&
        comparison_object.id == id
    end