views:

71

answers:

3

I am trying to find certain functions in the Rails api docs. For example, in the console I can type in:

ActiveRecord::Base.connection.tables

ActiveRecord::Base.connection.indexes("sometable")

and get a list of tables back and a list of indexes. I look in the Rails api doc http://api.rubyonrails.org/classes/ActiveRecord/Base.html but cannot find any reference to either of these.

I find connection() but the trail ends there! A little help figuring out the apis!

thanks...

+1  A: 

ActiveRecord::Base#connection internally relies on ActiveRecord::Base.connection (class method) which return a concrete implementation of the ActiveRecord::ConnectionAdapters::AbstractAdapter class.

You should search the #indexes method in your concrete adapter, for instance PostgreSQLAdapter#indexes.

RDoc doesn't provide any hint on the object type returned by a method call. You should dig into the source code or have a deep understanding of the package itself. There are alternatives such as Yard which generates a much more complete API documentation.

There's also the Rails Searchable API Doc Project which offers a searchable Rails API. You can enter the method name and have a list of all objects implementing that method.

Simone Carletti
+1  A: 
# in /vendor/rails/activerecord/lib/
$ ack -a 'def indexes'
active_record/connection_adapters/mysql_adapter.rb
449:      def indexes(table_name, name = nil)#:nodoc:

active_record/connection_adapters/postgresql_adapter.rb
664:      def indexes(table_name, name = nil)

active_record/connection_adapters/sqlite_adapter.rb
233:      def indexes(table_name, name = nil) #:nodoc:

active_record/connection_adapters/abstract/schema_statements.rb
28:      # def indexes(table_name, name = nil) end

active_record/schema_dumper.rb
166:      def indexes(table, stream)

Ack is better than grep, but you can use grep too if you want.

MBO
Why are you using -a? If you're just reflexively using -a because you have file extensions that don't match the ack built-ins, take the time to define your own in your .ackrc.
Andy Lester
@andy hmm, never bothered to do that, but I'll try :-)
MBO
+1  A: 

First you can browse through the Rails api doc. Most of the times this would answer your questions. But for some questions you get stuck as you noticed.
This is mostly a sign that it will become difficult to get the answer, since no documentation is present. So you have two options: do not use the method/feature you were looking information for, but use something else. Or get ready to spend a lot of time browsing through the Rails sources on github.

Use something like this Google query to get access to the particular github part. This will mostly send you to the correct file, but at some blob instead of the master, so find the same file in the master tree, so you know the thing you are looking for is available in your Rails (assuming that you are using the master/unmodified version) Next part is more difficult, you found the retrieve_connection method by now, but it uses another method retrieve_connection_pool, which is used in another file somewhere in the active_record/connection_adapters(/abstract) directory path. Which is using some other class, found in ..., and so on ...

This situation is mostly the case if you are browsing through the sources. After a while you will likely get the hang of it and become more used finding things you need. But it will always be a lot of work to find the answer this way.

So the choice is yours I guess.

PS Instead of using the Rails API you can try to type in some of your keywords ActiveRecord::Base.connection tables indexes in Google and hope someone wrote an article on these subjects and did the Rails source research for you.

Veger