# Get information from multiply tables

**URL:** <https://www.ruby-forum.com/t/get-information-from-multiply-tables/133521>\
**Category:** Rails\
**Created:** [April 1, 2008, 10:09am UTC](https://www.ruby-forum.com/t/get-information-from-multiply-tables/133521 "2008-04-01T10:09:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [April 1, 2008, 10:09am UTC](https://www.ruby-forum.com/t/get-information-from-multiply-tables/133521/1 "2008-04-01T10:09:34Z")

</div>

Hi,

These are my models:

* * *

class Advisor \< ActiveRecord::Base  
set\_table\_name “advisors”  
set\_primary\_key “id”

has\_many :advisee, :class\_name=\>“Student”, :foreign\_key=\>  
“advisor\_id”  
has\_many :second\_advisee, :class\_name=\>“Student”, :foreign\_key=\>  
“sub\_advisor”

end

* * *

class Student \< ActiveRecord::Base  
set\_table\_name “students”  
set\_primary\_key “id”

belongs\_to :advisor, :class\_name=\>“Advisor”, :foreign\_key =\>  
“advisor\_id”  
belongs\_to :second\_advisor, :class\_name=\>“Advisor”, :foreign\_key =\>  
“sub\_advisor”

has\_and\_belongs\_to\_many :courses, :class\_name=\>“Course”

end

* * *

class Course \< ActiveRecord::Base  
set\_table\_name “courses”  
set\_primary\_key “id”

has\_and\_belongs\_to\_many :students, :class\_name=\>“Student”

end

In my script/console\>

How can i find the first student?  
How can i find the first student and all her/his courses?

How can i find the first advisor ?  
How can i find the first advisor students?

Thanks…remco

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [April 1, 2008, 10:27am UTC](https://www.ruby-forum.com/t/get-information-from-multiply-tables/133521/2 "2008-04-01T10:27:53Z")

</div>

ok, let’s clean that up a bit, before we start

if you keep to naming conventions (as you do mostly)  
it’s not necessary to tell rails every foreign\_key or table name

class Advisor \< ActiveRecord::Base

has\_many :advisee, :class\_name=\>“Student”,  
:foreign\_key=\>“advisor\_id”  
has\_many :second\_advisee, :class\_name=\>“Student”,  
:foreign\_key=\>“sub\_advisor”

end

class Student \< ActiveRecord::Base

belongs\_to :advisor  
belongs\_to :second\_advisor, :class\_name=\>“Advisor”, :foreign\_key =\>  
“sub\_advisor”

has\_and\_belongs\_to\_many :courses

end

class Course \< ActiveRecord::Base

has\_and\_belongs\_to\_many :students, :class\_name=\>“Student”

end

> How can i find the first student?  
> @student = Student.find(:first)

> How can i find the first student and all her/his courses?  
> @student = Student.find(:first)  
> @student.courses

@student.courses will be an array of courses  
alternate:  
@courses = Student.find(:first).courses

to loop through them eg:  
@student.courses.each do |course|  
puts course.name  
end

> How can i find the first advisor ?  
> @advisor = Advisor.find(:first)

> How can i find the first advisor students?  
> @advisor.advisee

find offers lots of more search options, so you could find the first  
created student:  
@student = Student.find(:first, :order =\> “created\_at DESC”)

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [April 1, 2008, 11:38am UTC](https://www.ruby-forum.com/t/get-information-from-multiply-tables/133521/3 "2008-04-01T11:38:26Z")

</div>

He Thorsten,

Thanks!

Do you have some good url’s about this info?

remco

---

<div class="post-metadata">

**Author:** ![remco123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@remco123](https://www.ruby-forum.com/u/remco123)\
**Post date:** [April 1, 2008, 11:59am UTC](https://www.ruby-forum.com/t/get-information-from-multiply-tables/133521/4 "2008-04-01T11:59:20Z")

</div>

> Do you have some good url’s about this info?

sorry, not like a tutorial  
everything i did here is described in the Rails docs:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.
