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
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.
Have you tried using @facility.id ?
Thanks,
Jamie van Dyke
Fear of Fish
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. <
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