Forum: Ruby on Rails method to display the result of find (..) in a view

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.
Alec H. (Guest)
on 2007-03-22 11:45
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

2. 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

3. The view

<% columns_number = @order.length %>
<table cellspacing=0 class="listing">
<tr>
<th colspan="<%=columns_number%>"><%=Customer.table_name%></th></tr>
<tr>
<% @order.each do |column| %><th><%=column%></th><%end%>
<th colspan="3">&nbsp;</th></tr>
<% for customer in @customers %>
  <tr>
  <% @order.each do |value| %>
    <td><%=customer.send(value)%></td>
  <% end %>
</tr>
<%end%>
</table>

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?
Jon (Guest)
on 2007-03-22 11:56
(Received via mailing list)
On Mar 22, 9:45 am, Alec H. <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.
Alec H. (Guest)
on 2007-03-22 12:03
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)
This topic is locked and can not be replied to.