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 -- Posted via http://www.ruby-forum.com/.
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%> -- Posted via http://www.ruby-forum.com/.
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:> 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 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 -- Posted via http://www.ruby-forum.com/.
Stephen Bartholomew 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 -- Posted via http://www.ruby-forum.com/.
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 wrote:> Stephen Bartholomew 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 Bartholomew 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. > > Steveexcellent, any yeah you are right on the has_many. thanks scott -- Posted via http://www.ruby-forum.com/.