What syntax is this? belongs_to :Person

so when I’m reading the pick axe book second edition I don’t see
anything like the syntax you see people using in rails code.

Specifically when you see.

belongs_to :Person
has_many :Phones

etc

these are methods on ActiveRecord right?

Why is this invocation syntax never described in the Pick Axe book?

I do see things like attr_reader :some_attribute etc but you don’t
see it in the context
of invoking a method on your ancestor.

Can somebody cite me where this is described in the Pick Axe book?

On 25-May-06, at 9:04 PM, Robert N. wrote:

so when I’m reading the pick axe book second edition I don’t see
anything like the syntax you see people using in rails code.

The Pick Axe book is Programming Ruby' - you wantAgile Web
Development With Rails’

http://pragmaticprogrammer.com/titles/rails/index.html

Hoping this is at least a little helpful!

-Mike

On Thu, 2006-05-25 at 21:04 -0500, Robert N. wrote:

these are methods on ActiveRecord right?

Why is this invocation syntax never described in the Pick Axe book?

I do see things like attr_reader :some_attribute etc but you don’t
see it in the context
of invoking a method on your ancestor.

Can somebody cite me where this is described in the Pick Axe book?


Pick Axe book is about Ruby.

You are asking about Rails Framework item - ActiveRecord

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

Pick Axe book doesn’t cover Rails Framework

Craig

I have both books on bookshelf but I’m saying that I don’t see that
syntax convention in Programming Ruby.

I think you don’t understand.

When you write a subclass of ActiveRecord you’re still writing Ruby
code so I’m asking
why that syntax convention isn’t described or where it’s described in
the Pick Axe Book.

Specifically when you see.

belongs_to :Person
has_many :Phones

etc

these are methods on ActiveRecord right?

They are class methods on ActiveRecord::Base with the parentheses left
off

Why is this invocation syntax never described in the Pick Axe book?

I don’t think there’s a specific example, but it comes from two things
described in the pickaxe:

  1. You can leave off parentheses if there’s no confusing. Such as with
    Model.find :all
  2. All code is executable, so methods defined on the metaclass can be
    called in the class definition, it looks something like this:

class MyBase
def MyBase.do_stuff(thing)
end
end

class SubClass < MyBase
do_stuff :thing
end

Of course, this isn’t the only way to do it. I suggest you read about
metaclasses, I don’t have the book to hand so I can’t give you a page
reference.

Robert N. wrote:

I think you don’t understand.

When you write a subclass of ActiveRecord you’re still writing
Ruby code so I’m asking why that syntax convention isn’t
described or where it’s described in the Pick Axe Book.

Consider

has_many :Phones

You are calling the method has_many on the current ActiveRecord object.

The method has_many accepts the symbol name of an ActiveRecord object.

The Ruby syntax :Phone identifies a symbol. Symbol is Ruby Class.

This is referred to tangentially on page 179 of the latest Beta of AWDwR
and in the Pickaxe on page 638.

Using symbols, instead of strings, is a Rails convention.

Ray

On 25-May-06, at 9:21 PM, Robert N. wrote:

I have both books on bookshelf but I’m saying that I don’t see that
syntax convention in Programming Ruby.

Apologies, I misunderstood your post. I believe Phillip came much
closer. (:

I understand just fine. In fact, the answer to your question lies in the
very first sentence of the link that I included or on page 48 of David
Black’s Ruby for Rails book.

If you want to understand how ActiveRecord deals with the ‘macros’, you
do have the ‘source’.

Craig

On May 25, 2006, at 7:04 PM, Robert N. wrote:

these are methods on ActiveRecord right?
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Robert-

The attr_acessor :foo method described in the pickaxe is a class

method. In ruby classes are objects too so they can have methods that
run at class definition time. When you put attr_accessor :foo in a
ruby class all it really does is define these two instance methods:

attr_accessor :foo is the same as:

def foo
@foo
end

def foo=(value)
@foo = value
end

So in ActiveRecord, has_many :foos is actually a class method of

ActiveRecord::Base that dynamically defines a bunch of methods on
your model class to work with has_many relationships.

Cheers-
-Ezra