Forum: Ruby on Rails What syntax is this? belongs_to :Person

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert N. (Guest)
on 2006-05-26 06:07
(Received via mailing list)
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?
Mike O. (Guest)
on 2006-05-26 06:19
(Received via mailing list)
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 want `Agile Web
Development With Rails'

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

Hoping this is at least a little helpful!

-Mike
Craig W. (Guest)
on 2006-05-26 06:19
(Received via mailing list)
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/...

Pick Axe book doesn't cover Rails Framework

Craig
Robert N. (Guest)
on 2006-05-26 06:26
(Received via mailing list)
I have both books on bookshelf but I'm saying that I don't see that
syntax convention in Programming Ruby.
Robert N. (Guest)
on 2006-05-26 06:26
(Received via mailing list)
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.
Phillip H. (Guest)
on 2006-05-26 06:32
(Received via mailing list)
> 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.
Craig W. (Guest)
on 2006-05-26 06:39
(Received via mailing list)
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
Ray B. (Guest)
on 2006-05-26 07:00
(Received via mailing list)
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
Mike O. (Guest)
on 2006-05-26 07:10
(Received via mailing list)
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.  (:
Ezra Z. (Guest)
on 2006-05-26 07:13
(Received via mailing list)
On May 25, 2006, at 7:04 PM, Robert N. wrote:

> these are methods on ActiveRecord right?
> removed_email_address@domain.invalid
> 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
This topic is locked and can not be replied to.