Active Record HELP!


#1

Hello,

Can someone kindly tell what the heck creating a relationship does in
active record?

I understand the idea of joining the tables and such. But how do I use
it? Is it available in a scaffold? How about when I want to view a
record and want to see all the joined data from the other table? Do I
have to manually code the data I want from the other table? Does this
automatically make the linked data available in the data object?

Can we get real docs instead of API refs?

Thanks,
Phill


#2

I’m sure i speak for everyone when i highly recommend that you have a
look at getting the Agile book:
http://www.pragmaticprogrammer.com/titles/rails/index.html.

It’ll give you a good grounding in Rails and the concepts behind things
like Active record.

Steve


#3

https://addons.mozilla.org/firefox/1843/

From the site:

FireBug lets you explore the far corners of the DOM by keyboard or
mouse.
All of the tools you need to poke, prod, and monitor your JavaScript,
CSS,
HTML and Ajax are brought together into one seamless experience,
including
an error console, command line, and a variety of fun inspectors.

Visit the FireBug website for documentation, screen shots, and
discussion
forums:

http://www.joehewitt.com/software/firebug/

A quick overview of FireBug’s features:

  • Status bar icon shows you when there is an error in a web page
  • A console that shows errors from JavaScript and CSS
  • Log messages from JavaScript in your web page to the console (bye bye
    “alert debugging”)
  • An JavaScript command line (no more “javascript:” in the URL bar)
  • Spy on XMLHttpRequest traffic
  • Inspect HTML source, computed style, events, layout and the DOM

Works with:
Firefox 1.5 - 3.0 ALL


#4

Thanks. But that really doesn’t help. I have the book. I don’t see
anything that clearly states what s the result of linking the tables.

Are the columns from say table A available when I view a record from
table B that references table A?

Is my object now B.column1, B.column2, B.column3, A.column1 or what? If
NOT then what’s the point of creating the link?

Thanks,
Phill

On May 9, 2006, at 10:32 AM, Stephen B. wrote:

I’m sure i speak for everyone when i highly recommend that you have a
look at getting the Agile book:
http://www.pragmaticprogrammer.com/titles/rails/index.html.

It’ll give you a good grounding in Rails and the concepts behind things
like Active record.

Steve

Phillip Novess wrote:

Phill


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#5

See pages 233 and 237, they spell it out pretty clearly.

Ken


#6

You might want to start by looking at this screencast:

http://media.rubyonrails.org/video/rails_take2_with_sound.mov

Robert


#7

You should be able to access a parent via a child object. For example:

class Parent < ActiveRecord::Base
has_many :children
end

class Child < ActiveRecord::Base
belongs_to :parent
end

The ‘children’ table would have ‘parent_id’ column referencing the
‘parents’ table.

You can then reference the parent via the child:

child = Child.find(1)
child.parent.name -----> shows the parent’s name

The book does explain all this, but you may have to read through every
page as it’s not really a reference book (imo). The Rails recipes book
looks quite promising for a problem/solution approach.

Hope this helps,

Steve


#8

I don’t mean to sound rude, but you state that you have the book …
have you read it? Furthermore, have you ever built a database
application before? Please don’t take offense, but if you answered
no to these questions, then I think you’ll have some homework to do
before you are ready to dive into building a rails app.

It is stated quite clearly in the book, as well as the docs, that the
result of declaring an association in a model is that the elements in
the related model become available as methods of the class instance.
The method will access a single object for belongs_to or has_one
relationships, and an array of objects for has_many relationships.

One last thing - You’ll get faster and more appropriate responses if
you make your subject line meaningful. Especially on lists as busy
as this, generic subjects will be glossed over more often than not.

-Derrick S.


#9

Thanks. That makes sense. child.parent.name. Ah ha.

Thanks,
Phill

On May 9, 2006, at 12:13 PM, Stephen B. wrote:

You should be able to access a parent via a child object. For example:

class Parent < ActiveRecord::Base
has_many :children
end

class Child < ActiveRecord::Base
belongs_to :parent
end

The ‘children’ table would have ‘parent_id’ column referencing the
‘parents’ table.

You can then reference the parent via the child:

child = Child.find(1)
child.parent.name -----> shows the parent’s name

The book does explain all this, but you may have to read through every
page as it’s not really a reference book (imo). The Rails recipes book
looks quite promising for a problem/solution approach.

Hope this helps,

Steve

Phillip Novess wrote:

a look at getting the Agile book:

a record and want to see all the joined data from the other table?
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#10

Derrick,

Yeah I’ve made my living building web applications for the past 8
years. However, I use PHP and Oracle on a daily basis NOT Rails. I
don’t think anybody would argue that there is anything quite like
Rails. That’s what makes it interesting.

Also. I’m doing this in addition to my daily work in PHP so I get a
little confused. Ruby is 100% object oriented, PHP is not. I’m still
trying to sink my head around that concept alone. So please bare with
me. Sometimes I just need to ask what is meant by something otherwise
what’s the point of a list right?

I guess I’m spoiled with the excellent documentation resources at
php.net.

Thanks for the help. Very good points.

Thanks,
Phill

On May 9, 2006, at 12:13 PM, Derrick S. wrote:

I don’t mean to sound rude, but you state that you have the book …
have you read it? Furthermore, have you ever built a database
application before? Please don’t take offense, but if you answered no
to these questions, then I think you’ll have some homework to do before
you are ready to dive into building a rails app.

It is stated quite clearly in the book, as well as the docs, that the
result of declaring an association in a model is that the elements in
the related model become available as methods of the class instance.
The method will access a single object for belongs_to or has_one
relationships, and an array of objects for has_many relationships.

One last thing - You’ll get faster and more appropriate responses if
you make your subject line meaningful. Especially on lists as busy as
this, generic subjects will be glossed over more often than not.

-Derrick S.

On May 9, 2006, at 1:48 PM, Phillip Novess wrote:

Phill
It’ll give you a good grounding in Rails and the concepts behind

a record and want to see all the joined data from the other table?
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails