Forum: Ruby on Rails get foreign key table data

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
scott (Guest)
on 2006-04-26 15:15
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
scott (Guest)
on 2006-04-26 17:05
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.



<!--search for values from database-->
<% @org = Organisation.find_all_by_id(client.organisation_id)%>

<% if client.organisation_id != nil%>
 <td>
  <% @org.each do |org| %>
   <%= org.name %>
 <%end%>
</td>
<%else%>
 <td>
 </td>
<%end%>
Stephen B. (Guest)
on 2006-04-26 17:19
(Received via mailing list)
If you've got your object relationships set up, you should just be able
to access the organisation like this:

<%= client.organisation.name %>
Alan F. (Guest)
on 2006-04-26 17:24
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/As...

Alan
scott (Guest)
on 2006-04-26 17:31
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
Stephen B. (Guest)
on 2006-04-26 17:42
(Received via mailing list)
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
scott (Guest)
on 2006-04-26 17:47
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
This topic is locked and can not be replied to.