2

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 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't noticed this is a abstract class.
0

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.

1 Comment

Thanks. Now I can find proper .rb file.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.