Forum: Ruby on Rails looping

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Craig W. (Guest)
on 2006-02-23 17:59
(Received via mailing list)
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] ]

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] ]

# 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

What am I missing in fac_log_all ?

Alan F. (Guest)
on 2006-02-23 18:12
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

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.

Jamie van Dyke (Guest)
on 2006-02-23 18:18
(Received via mailing list)
Have you tried using     ?

Jamie van Dyke
Fear of Fish
Kevin O. (Guest)
on 2006-02-23 18:27
(Received via mailing list)
>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

<% for placement in facility.placements %>
  <%= 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 }

Clint P. (Guest)
on 2006-02-23 18:42
(Received via mailing list)
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,

needs an end quote after NULL.


On 23 Feb 2006 16:25:30 -0000, Kevin O. <
This topic is locked and can not be replied to.