1

I am trying to understand active record source code. The function includes seems to be a class method. Tracing with byebug I found the roots in lib/active_record/relation/query_methods.rb:137 like this

  def includes(*args)
    check_if_method_has_arguments!(:includes, args)
    spawn.includes!(*args)
  end  

This is an instance method and not a class method. But, let's say for a model named User we can do:

User.includes(:images)

How does it work in a class context?

shampoo
  • 1,173
  • 12
  • 22
  • Why do you say it's an instance method? – jvillian Mar 30 '18 at 18:32
  • because there is no `self` or class name before the method name. – shampoo Mar 30 '18 at 18:33
  • 3
    That doesn't *necessarily* mean it's an instance method. A method definition within a module behaves differently depending on whether that module is included or extended. – jvillian Mar 30 '18 at 18:35
  • So this is within a module right? – shampoo Mar 30 '18 at 18:37
  • Yes. Indeed it is. – jvillian Mar 30 '18 at 18:43
  • @jvillian: Every method in Ruby is always an instance method, there is only one kind of method. The question just is: what class does the instance method belong to? If it belongs to a singleton class, then we *call* it a singleton method, but that is just a shorthand way of talking about it, it is not actually a different concept. It is just an instance method which happens to belong to a singleton class. Likewise, if it belongs to a singleton class of an object which happens to be an instance of the `Class` class, then we may *talk* about it using the term "class method", but it isn't. … – Jörg W Mittag Mar 30 '18 at 21:24
  • … It's just a regular boring old instance method which belongs to a singleton class which happens to belong to an instance of the `Class` class. There is nothing special about the method. It's just that "instance method which belongs to a singleton class which belongs to an instance of the `Class` class" is cumbersome to say, so we say "class method" instead, *but knowing full well that the concept of a class method does not exist in Ruby*. It is just a jargon term for talking about particular kinds of instance methods. This is different from Java, for example, where instance methods, static … – Jörg W Mittag Mar 30 '18 at 21:27
  • … methods and constructors are three different things. In Ruby, they are not. There are no static methods, no class methods and no constructors. There are only instance methods. That is *extremely* important to understand for understanding Ruby. Ruby is, in fact, much simpler than it is often made out to be. – Jörg W Mittag Mar 30 '18 at 21:27
  • @JörgWMittag - Yessir, indeed. As discussed in the comments [here](https://stackoverflow.com/a/49075218/1359650). However, I suspect the OP was using `instance` and `class` methods more colloquially and so I answered as such. But, you're points are very well made and I concede happily. – jvillian Mar 30 '18 at 21:57

0 Answers0