# Newbie :: Help ! (database record compare)

**URL:** <https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563>\
**Category:** Rails\
**Created:** [April 10, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563 "2007-04-10T14:11:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 10, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/1 "2007-04-10T14:11:44Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 10, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/2 "2007-04-10T15:18:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 10, 2007, 3:28pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/3 "2007-04-10T15:28:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 10, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/4 "2007-04-10T14:49:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 10, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/5 "2007-04-10T15:44:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 10, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/6 "2007-04-10T15:32:51Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 19, 2007, 11:27am UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/7 "2007-04-19T11:27:44Z")

</div>

> " @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

---

<div class="post-metadata">

**Author:** ![aveo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@aveo](https://www.ruby-forum.com/u/aveo)\
**Post date:** [April 19, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/newbie-help-database-record-compare/91563/8 "2007-04-19T17:07:13Z")

</div>

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.
