Get foreign key table data

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

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%>

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

<%= client.organisation.name %>

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 (If
Amy’s site is back up) and
http://rubyonrails.org/api/classes/ActiveRecord/Associations/ClassMethods.html

Alan

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

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

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