Noob Question

Hi,

Total Ruby noob, trying to figure something out. Looking at the docs for ActiveRecord:Base, I see no mention of a "where" method in the method list. Yet one is available. I think that "where" is part of the ActiveRecord:QueryMethods.

So my question is twofold... first, how is "where" an available method on my object subclassed from ActiveRecord:Base? Is QueryMethods a mixin?

Second, looking at the documentation for ActiveRecordBase, how would I know that "where" is available or how to access it? I realize it's talked about in the text of the doc, but in general, how would I know what methods are available to an object from a mixin?

THanks in advance for any help.

So my question is twofold… first, how is “where” an available method

on my object subclassed from ActiveRecord:Base? Is QueryMethods a

mixin?

It’s inherited.

Second, looking at the documentation for ActiveRecordBase, how would I

know that “where” is available or how to access it? I realize it’s

talked about in the text of the doc, but in general, how would I know

what methods are available to an object from a mixin?

It’s inherited. If you are inheriting from ActiveRecord::Base it will be apart of your model. In every example in the documentation you are inheriting ActiveRecord::Base at the top of your class.

Thanks, B.

yeah, i find the docs fairly hard to navigate and I've been doing this for a while.

Once you figure out where the page is http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html

you'll see that none of the methods have any documentation. :-\

There is information on ARel queries here

and across several blog posts.

I wonder if documentation intentionally left out of the source to keep it smaller? Anyone have some insight here? Just today I was wondering where the controller request object methods are documented (ex: request.path.. maybe it is being deprecated?)

Right, but the docs for ActiveRecord::Base show that it derives from Object, so there's no "where" method in the chain anywhere. So I don't think it's inherited.

I find it really useful to query the objects themselves.

"hello".methods for example will how you methods available to call on that string, etc etc

Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now!

Start a Rails 3 app. Create a model called Foo without inheriting ActiveRecord.

class Foo

end

Start the Rails console:

rails console

Run the following command:

Foo.methods.sort

These are all the methods available to the class from just being in Rails. The “where” method is not among them.

Exit the console and edit the class to inherit ActiveRecord::Base.

class Foo < ActiveRecord::Base

end

Start the console again and re-run the command. In the bottom of that list you will find the method “where”, inherited from ActiveRecord::Base.

B.

Ok, now we're getting somewhere...

Looking at the source code for ActiveRecord::Base, I do NOT see "where" defined on ActiveRecord::Base. Nor is it defined in any superclass (Object). Nor do I see it defined in the docs (under methods).

So where the heck is it coming from? And if it's not defined in the docs, how would I know about it?

Loves me some puzzles.

OK, I think I have the answer. ActiveRecord::Base has a line in it:

delegate :select, :group, :order, :reorder, :except, :limit, :offset, :joins, :where, :preload, :eager_load, :includes, :from, :lock, :readonly, :having, :create_with, :to => :scoped

The method scoped() is defined in ActiveRecord::NamedScope::ClassMethods, and ActiveRecord::NamedScope is included in ActiveRecord::Base.

Far from obvious, but there it is.