views:

40

answers:

3

I'm just getting rolling with RoR so I'm sure this is pretty basic. Let's say I have two models: Account and Transaction

class Account < ActiveRecord::Base
    has_many :transactions
end

class Transaction < ActiveRecord::Base
    belongs_to :account
end

What methods (for each model) become available/are auto generated after I make this association?

Thanks

+1  A: 

It depends. Some methods (e.g. Account#transactions, Transaction#account) will be there from the get-go. Others will be created as needed (via a method_missing hook) such as dynamic finders. The exact list can depend on other factors, including things like acts_as, etc. used elsewhere.

Are you concerned about which ones are created or about what the full possibilities are?

-- MarkusQ

P.S. See here for more:

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

MarkusQ
I was just working with modifying the simple scaffolding to see what came up. I'm at the very basic levels right now, so going deep will probably hinder my progress.
Jason Punyon
So just look at the list I linked to, read down the page until it gets deeper than you want, and stop.
MarkusQ
What's the # by the way?
Jason Punyon
+1  A: 

run script/console from the root of your rails app and then you can explore all of the magic model methods like this:

> account = Account.new  
> account.methods
> ...[long list of methods]...
> transaction = Transaction.new  
> transaction.methods
> ...[long list of methods]...

You'll get a long list of all of the methods for the object including the generated ones. All of the methods will be listed without line breaks - and with 100+ methods it can be hard to read. You can use an .irbrc file in your home directory with some custom methods to format output in irb so it is easier to read.

inkdeep
A: 

going on inkdeep's answer : you can output something like this in your view so it's formatted:

@transaction.methods.each do |method| method + "
" end

I'm using HAML so syntax is a bit different, but just look up "do"

jim