Newbie :: Help ! (database record compare)

Hi all,

I’ve a two simple table,
resource

resource.id
resource.name
resource.contact

and

project

project.id
project.name
project.resource_id (id from resource table)

I want to display list of project that only belong to particular
resource.

my proj.rhtml is like

.

Projects

<% @project.each do |projects| %>
#####some condition to be written ####

<% end %>

ID

Project name

<%= link_to projects.id, :action => "show", :id => projects.id %> <%= projects.name %>
... .. . in controller i've written def proj @resources =Resource.find_all @project = Project.find_all end

which is showing all records from project table, How can write
condition so that it will display records for particular resource.
Please help me out to solve this
Thanks,

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

Project name

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.

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

Projects of Resource: <%= @resource.name %>

<% @resource.projects.each do |project| %> <% end %>

ID

Project name

<%= link_to project.id, :action => "show", :id => projects.id %> <%= project.name %>

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,

It worked, thanks a lot
same thing,
<% @resource.projects.each do |project| %>
That is all correct.
That was great. Please do a favor for me,
Please let me know about code, done in controller

" @met =Met.find(params[:id], :include => :projects) "
How this worked.

Thanks again,
Regards

Thorsten wrote:

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.

Thorsten
yes I’ve already setup association in model as

class Resource < ActiveRecord::Base
has_many :projects
end

class Project < ActiveRecord::Base
belongs_to :resource
end

Still same error.
Regards,

" @met =Met.find(params[:id], :include => :projects) "

Hi all,
now I’ve one more table

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 my project controller

############
def show
@resources =Resources.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”])

But doesnot work. Do I need to create association for this?
Please help me out.

Thanks

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.