# ActiveRecord get all columns including those belonging

**URL:** <https://www.ruby-forum.com/t/activerecord-get-all-columns-including-those-belonging/141480>\
**Category:** Rails\
**Created:** [June 26, 2008, 9:37am UTC](https://www.ruby-forum.com/t/activerecord-get-all-columns-including-those-belonging/141480 "2008-06-26T09:37:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan\_bayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@ryan\_bayona](https://www.ruby-forum.com/u/ryan_bayona)\
**Post date:** [June 26, 2008, 9:37am UTC](https://www.ruby-forum.com/t/activerecord-get-all-columns-including-those-belonging/141480/1 "2008-06-26T09:37:08Z")

</div>

Hi,  
Just new to Rails, and i have some questions :

How can i use ActiveRecord associations to retrieve all columns and rows  
including the columns and rows in the referenced table.?

\*SELECT parents.id, parents.parent\_name, kids.id, kids.parent\_id,  
kids.kid\_name  
FROM parents left join kids on parents.id = kids.parent\_id  
\*  
In PHP in can retrieve all rows returned by this query from all joined  
tables using mysql\_fetch\_array() and the output is  
_parents.id, parents.parent\_name, kids.id, kids.parent\_id,  
kids.kid\_name_

Is there an equivalent of this in RoR?

Here is the AR Associations that i want to retrieve all columns  
including  
those belonging to the referenced table

\*Code:

- = primary key

- = foreign key

Table : parents  
\*id  
parent\_name

Table : kids  
\*id  
+parent\_id  
kid\_name

class Parent \< ActiveRecord::Base  
has\_many :kids  
end

class Kid \< ActiveRecord::Base  
belongs\_to :parents  
end\*

Thanks!

---

<div class="post-metadata">

**Author:** ![ryan\_bayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@ryan\_bayona](https://www.ruby-forum.com/u/ryan_bayona)\
**Post date:** [June 26, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/activerecord-get-all-columns-including-those-belonging/141480/2 "2008-06-26T18:21:05Z")

</div>

On Jun 26, 2:00 am, “ryan bayona” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> Just new to Rails, and i have some questions :

Welcome!

> Is there an equivalent of this in RoR?

This is one of the changes in thinking when we switch to Rails. The  
belongs\_to and has\_many lets us not worry about the underlying queries  
too much.

For example, if the goal is to get a list of kids given a parent, then  
it’s this easy:

parent = Parent.find(1) # 1 is the parent\_id, use whatever you want  
parent.kids.size =\> returns the number of kids  
parent.kids.first.kid\_name # returns the name of the first kid

Another example:

kids = Parent.find(1).kids # get all kids for the parent  
names = kids.collect { |kid| kid.name } # names is now an array of  
names

You may also want to take a look at “Rails for PHP Programmers” by  
Pragmatic Press.

Hope this helps?

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

[purpleworkshops.com](http://purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![ryan\_bayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@ryan\_bayona](https://www.ruby-forum.com/u/ryan_bayona)\
**Post date:** [June 26, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/activerecord-get-all-columns-including-those-belonging/141480/3 "2008-06-26T19:24:27Z")

</div>

Also try

parent = Parent.find(1, :include =\> :kids)

Then subsequent calls like Jeff mentions won’t actually go to the DB  
(they are already loaded)…

---

<div class="post-metadata">

**Author:** ![ryan\_bayona](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@ryan\_bayona](https://www.ruby-forum.com/u/ryan_bayona)\
**Post date:** [June 27, 2008, 12:14am UTC](https://www.ruby-forum.com/t/activerecord-get-all-columns-including-those-belonging/141480/4 "2008-06-27T00:14:58Z")

</div>

Im going to try your suggestions. I will also find that book you  
mentioned…,

Thank you so much!
