# Get foreign key table data

**URL:** <https://www.ruby-forum.com/t/get-foreign-key-table-data/58835>\
**Category:** Rails\
**Created:** [April 26, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835 "2006-04-26T13:15:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 26, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835/1 "2006-04-26T13:15:19Z")

</div>

Hi

Iâ??m trying to bring across all related data.

My table clients has a foreign key field that stores the id of an  
organization

How can I grab the details of the organization to use in the clients  
show.rhtml file?

Thanks  
Scott

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 26, 2006, 3:05pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835/2 "2006-04-26T15:05:00Z")

</div>

whe way I have done this in the end is below, all this code is in the  
.rhtml file, so it doesnt seem right to me. I would rather gain the data  
in clients\_controller.rb

If anyone can think of any better ways, please let me know.

\<% @org = Organisation.find\_all\_by\_id(client.organisation\_id)%\>

\<% if client.organisation\_id != nil%\>

 \<% @org.each do |org| %\> \<%= org.name %\> \<%end%\> \<%else%\> \<%end%\>

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 26, 2006, 3:19pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835/3 "2006-04-26T15:19:55Z")

</div>

If you’ve got your object relationships set up, you should just be able  
to access the organisation like this:

\<%= client.organisation.name %\>

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 26, 2006, 3:24pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835/4 "2006-04-26T15:24:39Z")

</div>

scott wrote:

> whe way I have done this in the end is below, all this code is in the  
> .rhtml file, so it doesnt seem right to me. I would rather gain the data  
> in clients\_controller.rb
> 
> If anyone can think of any better ways, please let me know.

class Client \< ActiveRecord::Base  
belongs\_to :organisation  
end

class Organisation \< ActiveRecord::Base  
has\_one :client (or has\_many :clients)  
end

def actionmethod  
the\_client = Client.find(:first)  
the\_org = theCLient.organisation  
end

Please see [http://www.slash7.com/cheats/activerecord\_cheatsheet.pdf](http://www.slash7.com/cheats/activerecord_cheatsheet.pdf) (If  
Amy’s site is back up) and  
[http://rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html](http://rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html)

Alan

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 26, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835/5 "2006-04-26T15:31:41Z")

</div>

Stephen B. wrote:

> If you’ve got your object relationships set up, you should just be able  
> to access the organisation like this:
> 
> \<%= client.organisation.name %\>

seems a lot better, however im just getting the error message

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.name

my models are shown below for these 2 tables

class Organisation \< ActiveRecord::Base

has\_one :client  
acts\_as\_dropdown :order =\> ‘name’

#validate form input  
validates\_presence\_of :name, :address1, :address2, :postcode,  
:officePhone  
validates\_numericality\_of :dailyMainRate, :dailyDevRate  
validates\_uniqueness\_of :name, :on =\> :create  
end

class Client \< ActiveRecord::Base

belongs\_to :organisation

validates\_presence\_of :forename, :surname  
end

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 26, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835/6 "2006-04-26T15:42:01Z")

</div>

Has the client got an organisation assigned to it? You may have to  
check:

\<% if client.organisation %\>  
\<%= client.organisation.name %\>  
\<% end %\>

Also, should the relationship from Organisation to Client not be a  
has\_many? This is a design thing so i might be wrong, but i would has  
assumed that an Organisation would have many Clients, not just one.

Steve

---

<div class="post-metadata">

**Author:** ![scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@scott](https://www.ruby-forum.com/u/scott)\
**Post date:** [April 26, 2006, 3:47pm UTC](https://www.ruby-forum.com/t/get-foreign-key-table-data/58835/7 "2006-04-26T15:47:49Z")

</div>

Stephen B. wrote:

> Has the client got an organisation assigned to it? You may have to  
> check:
> 
> \<% if client.organisation %\>  
> \<%= client.organisation.name %\>  
> \<% end %\>
> 
> Also, should the relationship from Organisation to Client not be a  
> has\_many? This is a design thing so i might be wrong, but i would has  
> assumed that an Organisation would have many Clients, not just one.
> 
> Steve

excellent,

any yeah you are right on the has\_many.

thanks  
scott
