# Show info from table wich is joined

**URL:** <https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378>\
**Category:** Ruby\
**Created:** [November 6, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378 "2007-11-06T15:53:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [November 6, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378/1 "2007-11-06T15:53:33Z")

</div>

Hi,

\<% @user.email %\>  
…is working

\<% @user.educations do |education| %\>

- \<%= education.name%\>
  
…is NOT working.

This is because there is a join-table involved…below the mirgation

class AddTableEducationsUsers \< ActiveRecord::Migration  
def self.up  
create\_table :educations\_users do |t|  
t.column :education\_id, :integer  
t.column :user\_id, :integer

end  
add\_index “educations\_users”, “education\_id”  
add\_index “educations\_users”, “user\_id”

end  
def self.down  
drop\_table :educations\_users  
end  
end

I want on the user-page the occupations of the user…any suggestions??

remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [November 7, 2007, 8:38am UTC](https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378/2 "2007-11-07T08:38:51Z")

</div>

Did you setup the AR models?

User  
has\_many :education

Education  
belongs\_to :user

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [November 7, 2007, 7:22pm UTC](https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378/3 "2007-11-07T19:22:15Z")

</div>

Jacob Basham wrote:

> Did you setup the AR models?
> 
> User  
> has\_many :education
> 
> Education  
> belongs\_to :user

Hi Jacob,

My models are…

User  
has\_and\_belongs\_to\_many :educations

Education  
has\_and\_belongs\_to\_many :users

Because a user can have many educations and eductions can have many  
users.

remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [November 7, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378/4 "2007-11-07T19:31:12Z")

</div>

On Nov 7, 2007 12:22 PM, Remco S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> users.  
> Are you trying to move away from the DB relational model (i.e. you  
> plan to alter the DB), or just trying to get Ruby to work with it?

By the sound of it, you have something in the DB that looks like  
(forgive the crude notation)…

Users ← Expertise → Educations

3 tables?

Todd

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [November 7, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378/5 "2007-11-07T20:18:50Z")

</div>

Todd B. wrote:

> On Nov 7, 2007 12:22 PM, Remco S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > users.  
> > Are you trying to move away from the DB relational model (i.e. you  
> > plan to alter the DB), or just trying to get Ruby to work with it?
> 
> By the sound of it, you have something in the DB that looks like  
> (forgive the crude notation)…
> 
> Users ← Expertise → Educations
> 
> 3 tables?
> 
> Todd

I have 3 tables

users  
educations  
educactions\_users

The educactions\_users is my join table, with the columns users\_id and  
educations\_id.

A part of the user-edit-view looks like this, where the user can  
specified there educations. This works…after getting grey hair!!

#### educations
 \<% for education in Education.find(:all) %\> 
 \<%= check\_box\_tag "user[education\_ids][]", education.id, @user.educations.include?(education) %\> \<%= education.name%\> 
 \<% end %\>

But…

In the show-view i want a list of educations wich the user has selected.  
I thought this.

#### Educations

 \<% @user.educations do |education| %\> - \<%= education.name%\>
 \<% end %\> 

This get empty value…

This is my problem…

Grtz…remco

---

<div class="post-metadata">

**Author:** ![remco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9d8465/32.png) [@remco](https://www.ruby-forum.com/u/remco)\
**Post date:** [November 8, 2007, 12:33am UTC](https://www.ruby-forum.com/t/show-info-from-table-wich-is-joined/119378/6 "2007-11-08T00:33:38Z")

</div>

On Nov 7, 2007 1:18 PM, Remco S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Users ← Expertise → Educations
> 
> @user.educations.include?(education) %\>
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Have you tried specifying the word “through” in your rails models.  
This works for me, for example…

class User \< ActiveRecord::Base  
has\_many :memberships  
has\_many :groups, :through =\> :memberships  
end

class Group \< ActiveRecord::Base  
has\_many :memberships  
has\_many :users, :through =\> :memberships  
end

class Membership \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :group  
end

hth a little,  
Todd
