# Looping

**URL:** <https://www.ruby-forum.com/t/looping/72394>\
**Category:** Rails\
**Created:** [February 23, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/looping/72394 "2006-02-23T16:59:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 23, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/looping/72394/1 "2006-02-23T16:59:53Z")

</div>

I have a form that prints fine for one particular criteria…  
facility\_id = “X”

controller code looks like this…

def fac\_log  
@facility = Facility.find(params[:report][:facility\_id])  
@placement = Placement.find(:all,  
:conditions =\> [ "facility\_id = ? and discharge\_date IS NULL,  
params[:report][:facility\_id] ]  
)  
end

I can print out the form with…  
\<%= render :partial =\> ‘fac\_log’ %\>

now I want to print for all facilities… controller code looks like…

def fac\_log\_all  
@facility = Facility.find(:all)  
@placement = Placement.find(:all,  
:conditions =\> [ "facility\_id = ? and discharge\_date IS NULL,  
@facility[:id] ]  
)  
end

# note, I am not certain that ‘@facility[:id]’ will work but I need the

‘id’ from the the @facility

anyway, the form that prints fine as above for 1 specfic facility  
doesn’t work with this…

\<% for facility in @facility %\>  
\<%= render :partial =\> ‘fac\_log’ %\>  
\<% end %\>

as it results in an error…that I have ‘nil’ object (undoubtedly  
@facility[:id]

What am I missing in fac\_log\_all ?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 23, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/looping/72394/2 "2006-02-23T17:12:30Z")

</div>

Craig W. wrote:

> def fac\_log\_all  
> @facility = Facility.find(:all)  
> @placement = Placement.find(:all,  
> :conditions =\> [ "facility\_id = ? and discharge\_date IS NULL,  
> @facility[:id] ]  
> )  
> end

In this method @facility is actually an array of Facility objects, not a  
single facility object.

If Facility has\_many placements, you can probably do

Facility.find(:all).each |f| do  
f.placements.each |p| do  
# do something with f and p  
end  
end

Or more likely

@all\_facilities = Facility.find(:all)  
render :partial =\> ‘fac\_log’, :collection =\> @all\_facilities

This will render the template once for each facility, creating a  
variable called ‘facility’, from which you can call facility.placements  
in the template.

A.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 23, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/looping/72394/3 "2006-02-23T17:18:07Z")

</div>

Have you tried using @facility.id ?

Thanks,  
Jamie van Dyke  
Fear of Fish

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 23, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/looping/72394/4 "2006-02-23T17:42:28Z")

</div>

Assuming the code you sent is directly from your app, you are looking at  
another problem. The line:

:conditions =\> [ "facility\_id = ? and discharge\_date IS NULL,  
@facility[:id]  
]

needs an end quote after NULL.

Clint

On 23 Feb 2006 16:25:30 -0000, Kevin O. \<

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 23, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/looping/72394/5 "2006-02-23T17:27:15Z")

</div>

> controller code looks like this…
> 
> def fac\_log\_all  
> @facility = Facility.find(:all)  
> @placement = Placement.find(:all,  
> :conditions =\> [ "facility\_id = ? and discharge\_date IS NULL,  
> @facility[:id] ]  
> )  
> end

This won’t work because @facility is an array of results, and  
@facility[:id] won’t know which one to look at.

My suggestion would be to modify your partial so it does something like  
this…

\<% for placement in facility.placements %\>  
\<%= placement.name if placement.discharge\_date %\>  
\<% end %\>

Assuming you have your relationships set up right this should work.  
If you really need to find a list of all the placements with a null  
discharge date, you could create a method in your model that does the  
lookup for you.

def find\_not\_discharged  
placements.reject {|p| p.discharge\_date.nil? == false }  
end

\_Kevin
