Hi all, I've a three simple table, resource ======= resource.id resource.name resource.contact project ====== project.id project.name project.resource_id (id from resource table) and training ====== training.id training.name training.resource_id (id from resource table) I want to display list of projects and trainings that only belong to particular resource. #in project Controller def proj @resource =Resource.find(params[:id], :include => :projects) end So it is showing projects associated with resource, But how can show trainings associated with resource on same page. I was trying something like #### @train = Training.find(@params["id"]) #### in proj But doesnot work. Do I need to create association for this? my #proj.rhtml <h1>Projects of Resource: <%= @resource.name %></h1> <table border="1"> <tr> <td width="30%"><p align="center"><i><b>ID</b></i></td> <td width="20%"><p align="center"><i><b>Project name</b></i></td> <td width="20%"><p align="center"><i><b>Training name</b></i></td> </tr> <% @resource.projects.each do |project| %> <tr> <td><%= link_to project.id, :action => "show", :id => projects.id %></td> <td><%= project.name %></td> <td><%= training.name %></td> </tr> <% end %> </table> Please help me out. Thanks,
on 2007-04-19 13:24
on 2007-04-19 15:21
@resource =Resource.find(params[:id], :include =>[:projects, :trainings] As long as your associations are set in the models, this will work. class Resource < ActiveRecord::base has_many :trainings has_many :projects end That will work. Rails has an api at http://api.rubyonrails.com where this is all documented. You should make sure to give that a look if you have additional questions.