# Method to display the result of find (..) in a view

**URL:** <https://www.ruby-forum.com/t/method-to-display-the-result-of-find-in-a-view/89728>\
**Category:** Rails\
**Created:** [March 22, 2007, 10:45am UTC](https://www.ruby-forum.com/t/method-to-display-the-result-of-find-in-a-view/89728 "2007-03-22T10:45:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jubei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jubei](https://www.ruby-forum.com/u/jubei)\
**Post date:** [March 22, 2007, 10:45am UTC](https://www.ruby-forum.com/t/method-to-display-the-result-of-find-in-a-view/89728/1 "2007-03-22T10:45:05Z")

</div>

I’ve been trying to write a method that effectively displays the result  
of a find() for say… my Customer model.

What i’ve come up with is this:

1. The controller

My main controller first makes the query via the model

class MainController \< ApplicationController

def index  
order = [‘lastname’,‘firstname’,‘father’,‘address1’, …  
,‘insuranceid’]  
@customers = Customer.find\_customers(order) #passes the columns in  
the order  
#we want them shown  
#Without this (using  
attributes)  
#the attributes method  
returned  
#the columns in random  
order!

```
@order = order #also passes them to the

```

view  
end

end

1. The Customer model

class Customer \< ActiveRecord::Base

def self.find\_customers(order)  
selection =""  
order.each do |column|  
selection += column+"," # appends members of the order array to the  
string  
end  
resultset = find(:all, :select =\> [selection.chop]) #queries the DB  
resultset #returns the array of Customer objects limited by :select  
end

end

1. The view

\<% columns\_number = @order.length %\>

\<% @order.each do |column| %\>\<%end%\> \<% for customer in @customers %\> \<% @order.each do |value| %\> \<% end %\> \<%end%\>

| \<%=Customer.table\_name%\> |
| --- |
| \<%=column%\> | &nbsp; |
| --- | --- |
| \<%=customer.send(value)%\> |

My objective is to make a view method that renders ANY type of object  
array or ANY other object type for that matter. The standard scaffold  
method doesn’t work for me because it uses “for column in  
Customer.content\_columns” which are the columns defined in the model,  
ALL of them, whereas I limit the columns via :select.

Since I’m a newbie Ruby/Rails coder I’m almost certain there’s a better  
way to make such a “scaffold-like” view that can render any array of db  
objects. Could anybody contribute to this code an help improve it? Is  
there a more “rails” way to do what I’m trying to do?

---

<div class="post-metadata">

**Author:** ![jubei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jubei](https://www.ruby-forum.com/u/jubei)\
**Post date:** [March 22, 2007, 10:56am UTC](https://www.ruby-forum.com/t/method-to-display-the-result-of-find-in-a-view/89728/2 "2007-03-22T10:56:19Z")

</div>

On Mar 22, 9:45 am, Alec H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’ve been trying to write a method that effectively displays the result  
> of a find() for say… my Customer model.

…

> My objective is to make a view method that renders ANY type of object  
> array or ANY other object type for that matter. The standard scaffold  
> method doesn’t work for me because it uses “for column in  
> Customer.content\_columns” which are the columns defined in the model,  
> ALL of them, whereas I limit the columns via :select.

Have you looked at ajax\_scaffold? It doesn’t have anything in for  
automatically generating the display from your select statement, but  
it will allow you to choose which fields are displayed from your  
model.

---

<div class="post-metadata">

**Author:** ![jubei](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jubei](https://www.ruby-forum.com/u/jubei)\
**Post date:** [March 22, 2007, 11:03am UTC](https://www.ruby-forum.com/t/method-to-display-the-result-of-find-in-a-view/89728/3 "2007-03-22T11:03:45Z")

</div>

Jon wrote:

> Have you looked at ajax\_scaffold? It doesn’t have anything in for  
> automatically generating the display from your select statement, but  
> it will allow you to choose which fields are displayed from your  
> model.

I have. Unfortunately it doesn’t allow as much flexibility as I wanted  
so I decided to write one myself. Also ajax\_scaffold and Activescaffold  
for that matter have code hidden in a library somewhere and I couldnt  
get static code (like when you generate a scaffold with a generator)
