# Trying to make new instance variables for each item in render :partial

**URL:** <https://www.ruby-forum.com/t/trying-to-make-new-instance-variables-for-each-item-in-render-partial/189395>\
**Category:** Rails\
**Created:** [May 31, 2010, 7:27am UTC](https://www.ruby-forum.com/t/trying-to-make-new-instance-variables-for-each-item-in-render-partial/189395 "2010-05-31T07:27:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 31, 2010, 7:27am UTC](https://www.ruby-forum.com/t/trying-to-make-new-instance-variables-for-each-item-in-render-partial/189395/1 "2010-05-31T07:27:24Z")

</div>

I have a list of people I am using as the collection for a  
render :partial. This is for a list of users that is being made into a  
pdf. I need to do 3 find calls to find out their attendance for the  
last 3 years. The only way I can find to do this is to have the print  
function call its view and have the find code in there, but I know  
that’s wrong… Please point me in the right direction. Maybe a way to  
put the login in the controller ?? Is there a way to add some logic to  
the beginning of a render ??

def print  
debugger  
@today = Date.today  
@year = @today.year  
@households = Household.find(:all, :order =\> “last\_name,  
first\_name”)  
@churches = Church.find(:all, :order =\> “name”).map {|u| [u.name, u.id]}

```
respond_to do |format|
 # format.html # index.html
# format.xml { head :ok }

```

# format.pdf { send\_data render\_to\_pdf({ :action =\>

‘log’, :layout =\> ‘pdf\_report’ }) }  
format.pdf { send\_data render\_to\_pdf( :action =\> ‘print’,  
:layout =\>  
‘pdf\_report’),  
:filename =\> "households  
for " + Time.now.to\_s(:long) + “.pdf” }  
end  
end

```
<% @households.each do |@household| %>
  <% @thisyear =

```

Visit.find\_or\_create\_by\_household\_id\_and\_year(:household\_id =\>  
@household, :year =\> @year) %\>  
\<% @lastyear =  
Visit.find\_or\_create\_by\_household\_id\_and\_year(:household\_id =\>  
@household, :year =\> @year-1) %\>  
\<% @twoyearsago =  
Visit.find\_or\_create\_by\_household\_id\_and\_year(:household\_id =\>  
@household, :year =\> @year-2) %\>  
\<%= render :partial=\>“print” %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [May 31, 2010, 10:28am UTC](https://www.ruby-forum.com/t/trying-to-make-new-instance-variables-for-each-item-in-render-partial/189395/2 "2010-05-31T10:28:16Z")

</div>

On May 31, 6:25 am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a list of people I am using as the collection for a  
> render :partial. This is for a list of users that is being made into a  
> pdf. I need to do 3 find calls to find out their attendance for the  
> last 3 years. The only way I can find to do this is to have the print  
> function call its view and have the find code in there, but I know  
> that’s wrong… Please point me in the right direction. Maybe a way to  
> put the login in the controller ?? Is there a way to add some logic to  
> the beginning of a render ??

I think you want to restructure your code a bit. For example, create  
an instance method on household for retrieving a view object given a  
year. Assuming you had a helper function or a partial for displaying  
views your partial for displaying a household wouldn’t need to be much  
more complicated than

\<%= format\_view household.view\_for\_year @year %\>

\<%= format\_view household.view\_for\_year @year - 1%\>

\<%= format\_view household.view\_for\_year @year - 2%\>

fred
