2
votes

I wanted to know how ActiveRecord::Base.connection.execute method is defined.

So I've checked source code but I couldn't understand what is going on.

# Executes the SQL statement in the context of this connection.
def execute(sql, name = nil)
end
undef_method :execute

https://github.com/rails/rails/blob/c13284131511fb7871a85659dd0b5e821d9ad29b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb#L55

Perhaps the method is defined another place dynamically. How can I find place where the method is described?

3

3 Answers

1
votes

The method you show is defined in the module DatabaseStatements which is included into the class AbstractAdapter (connection_adapters/abstract_adapter.rb).

AbstractAdapter simply serves as a base class for the various specialised database adapters for the different database servers Rails interoperates with; it's not intended to be instantiated on its own. For instance, the definition of execute for PostgreSQL databases is in postgresql/database_statements.rb, as part of class PostgreSQLAdapter < AbstractAdapter.

0
votes

They are defined in respective adapters.

The adapters are in the following directory names as *_adapter.rb:

activerecord-x.x.x/lib/active_record/connection_adapters/

You can see the the definition of the execute method inside those files. like: mysql_adapter.rb, postgresql_adapter.rb etc.

0
votes

To know how ActiveRecord::Base.connection.execute method is defined you should look in the connection adapter class you're using.
For instance, in case you're using mysql db (via mysql2 gem) you'll find the execute method definition you're using here: activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb#206