@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.
On 4/19/07, Rahul Ha
<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>
wrote:>
>
> 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,
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---