Newbie :: Help !!! (database record compare)

Hi all,

I've a two simple table, resource

First of all, the condition you want to implement should be in the controller, not in the view. You can to use eager loading and a condition to only load the resource in question (most likely defined by a id value in the request params) and the associated projects in one SQL query with Active Record. then in your view you loop through those projects and show them

it would go something like this:

#Controller def proj     @resource =Resource.find(params[:id], :include => :projects)   end

#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> </tr> <% @resource.projects.each do |project| %> <tr> <td><%= link_to project.id, :action => "show", :id => projects.id %></td> <td><%= project.name %></td> </tr> <% end %> </table>

NOTE: of course i hope you have set up the correct associations in the models, which are nessessary for the controller stuff:

class Resource < ActiveRecord::Base   has_many :projects end

class Project < ActiveRecord::Base   belongs_to :resource end

Thorsten, Thank you for your response.That is really appreciated. I've done changes stated by you,

#Controller def proj     @resource =Resource.find(params[:id], :include => :projects)   end

#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> </tr> <% @resource.projects.each do |project| %> <tr> <td><%= link_to project.id, :action => "show", :id => projects.id %></td> <td><%= project.name %></td> </tr> <% end %> </table>

NOTE: of course i hope you have set up the correct associations in the models, which are nessessary for the controller stuff:

class Resource < ActiveRecord::Base   has_many :projects end

class Project < ActiveRecord::Base   belongs_to :resource end

for Projects of Resource: <%= @resource.name %>, it is taking correct name from database. But at <% @resource.projects.each do |project| %> it is showing error as "undefined local variable or method `projects' for #<#<Class........." I am unable to sort out it, please help me out. Thanks

well that error is most likely the result of a missing association. Have you set up the has_many :projects association as explained in my previous post? without that, the .projects method of the resource instance won't be available and result in this error.

yes you have to create an association fort this, just like with the porjects table/model

and then in your controller you can do:

@resource =Resources.find(params[:id], :include => [:projects, :trainings])

this will include the associated projects and trainings as well.

an you can use in your view :

@resource.trainings.each do |training|

blbla

end

just like with projects.