Forum: Ruby on Rails looping

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-23 16: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] ]
      )
  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
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-02-23 17:12
Craig White 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.
68d3f48595af5d48db8de48cf9349e71?d=identicon&s=25 Jamie van Dyke (Guest)
on 2006-02-23 17:18
(Received via mailing list)
Have you tried using @facility.id     ?

Thanks,
Jamie van Dyke
Fear of Fish
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-02-23 17: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
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
Aeb5810616aafc577c999d769d3f1bd2?d=identicon&s=25 Clint Pidlubny (Guest)
on 2006-02-23 17: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,
@facility[:id]
]

needs an end quote after NULL.

Clint

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