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"> </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?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Mar 22, 9:45 am, Alec Higgins <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Alec Higgins
2007-Mar-22 10:03 UTC
Re: method to display the result of find (..) in a view
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) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---