Dave Isaacs
Dave Isaacs

Reputation: 4539

How do I discover available methods in Rails documentation?

I am new to Rails, and I am finding it extremely opaque. I have a copy of the latest Agile Web Development with Rails, but my worry is that without this book I would be completely lost.

For example, following the depot example in the book, when it comes to adding validation to the model, you do

class Product < ActiveRecord::Base

  validates :title, :description, :image_url, :presence => true

end

Seems easy enough, except that without the AWDwR book I would never have figured this out. There is nothing in the ActiveRecord::Base documentation that mentions the validates method.

It seems to me that with Rails you are just supposed to mysteriously know what methods are available at any given point in a project. But if you don't know, how are you supposed to find out (apart from memorizing a 500+ page book)?

I can phrase the question another way: In my Product class, I have available to me a method named validates. How is this method made available to my Products class? Even knowing that it is defined in ActiveModel::Validations::ClassMethods (I know this because I looked it up) I cannot figure out how it has been made available to my Product class.

Upvotes: 4

Views: 408

Answers (4)

MoFlo
MoFlo

Reputation: 559

Rails ActiveRecord does support model introspection of columns and methods, just use the following

$ rails console
1.9.3> Product.columns
1.9.3> => [#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007fe853d2c1f0 @name="id", @sql_type="integer", @null=false, ...

1.9.3> Product.methods
1.9.3> => [:_validators, :before_add_for_memberships?, :before_add_for_memberships=, :before_add_for_memberships, :after_add_for_memberships?, ...

That would theoretically allow you to discover likely methods (or columns) which may be of interest and then you can use the API doc sources referenced in other answers.

Upvotes: 1

Dave Isaacs
Dave Isaacs

Reputation: 4539

It's been almost a year, and I can now look back and say that the best resources I found for learning rails are the Rails Guides at http://guides.rubyonrails.org/. They tie everything together very nicely, provide some examples, and give me an entry point into the API documentation (as opposed to flailing around randomly like I did when I first started).

Upvotes: 0

Anthony Bishopric
Anthony Bishopric

Reputation: 1296

the authoritative site too - http://api.rubyonrails.org

Upvotes: 1

Mike Lewis
Mike Lewis

Reputation: 64137

I use http://railsapi.com/ on a daily basis, I hope you find it helpful as well!

Upvotes: 1

Related Questions