On Aug 25, 4:00 am, Colin Law <[email protected]> wrote:
> On 25 August 2010 08:02, Frederick Cheung <[email protected]> wrote:
>
>
>
> > On Aug 25, 7:53 am, Bob Smith <[email protected]> wrote:
>
> >> The problem I'm having is in combining these two name lists into one
> >> list, then putting it in alphabetical order.
> >> Name is for head of household, and sname is for a second person,
> >> either a wife or husband. These are two fields in the same record.
> >> Combining the lists and putting them in alphabetical order will show a
> >> full list to match whoever comes for the food. This program is for a
> >> local foodbank.
>
> > Sounds like Ar Chron's code would do it. Did you try his suggestion ?
>
> I think I have just realised what the OP is after, he wants each
> record to appear twice in the list, once showing name and once showing
> sname with the two record sets interleaved so that the displayed names
> are in alpha order.
> OP is that correct?
>
> Colin
Ok, Bob,
The problem now seems to be that there is a Household model which
contains two fields, name and sname, each of which is the full name
(hopefully in the format of last_name, first_name) of a Person in the
Household (although it now appears that the question is limited in
scope to using a "flat file" which always includes two People per
Household). While I would suggest using a second, Person, model,
whereby each Household could include one or more "authorized" People,
that does not appear to be the question.
Assuming that I now understand the Household model, the question
appears to be, "How do I create a view which interleaves the names
(name and sname) from multiple Household records, alphabetically, with
two columns per row ("aname" and "id") from the households table,
where "aname" is either "name" or "sname" from the original table."
Given the foregoing (and without adding a Person model and
relationships), the way I would implement the controller would be to
create a new array of hashes of the form {:aname, :id}, then add an
element to the array from name/id and sname/id hash for each row in
the households table. Then, in the view, I would simply sort the new
array by aname, and display the sorted array.
Assuming the following Household records:
id name sname
1 Doe, John Doe, Joan
2 Smith, Sam Doe, Jill
3 Adams, Art Little, Louise
4 Jones, Tom Doe, Jane
The sorted array would display, as follows:
Recipients
Name Household ID
Adams, Art 3
Doe, Jane 4
Doe, Jill 2
Doe, Joan 1
Doe, John 1
Jones, Tom 4
Little, Louise 3
Smith, Sam 2
For simplicity (without dealing with routes.rb), you can provide the
following code in the households_controller to show the above in the
view (index.html.erb):
def index
@households = Household.all
@sorted = Array.new
for n in @households do
@sorted << { :aname => n.name, :id => n.id }
@sorted << { :aname => n.sname, :id => n.id }
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @households }
end
end
Then, in the view, index.html.erb:
<h1>Listing Households</h1>
<table>
<tr>
<th>id</th>
<th>Name</th>
<th>Sname</th>
</tr>
<% @households.each do |household| %>
<tr>
<td><%=h household.id %></td>
<td><%=h household.name %></td>
<td><%=h household.sname %></td>
<td><%= link_to 'Show', household %></td>
<td><%= link_to 'Edit', edit_household_path(household) %></td>
<td><%= link_to 'Destroy', household, :confirm => 'Are you
sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<h1>Listing Recipients</h1>
<table>
<tr>
<th>Name</th><th>Household ID</th>
</tr>
<% @sorted.sort_by { |n| n[:aname] }.each do |n| %>
<tr>
<td><%= n[:aname] %></td><td><%= n[:id] %>
</tr>
<% end %>
</table>
<%= link_to 'New household', new_household_path %></table>
Thereby, providing the following output in the view:
Listing Households
id Name Sname
1 Doe, John Doe, Joan Show Edit Destroy
2 Smith, Sam Doe, Jill Show Edit Destroy
3 Adams, Art Little, Louise Show Edit Destroy
4 Jones, Tom Doe, Jane Show Edit Destroy
Listing Recipients
Name Household ID
Adams, Art 3
Doe, Jane 4
Doe, Jill 2
Doe, Joan 1
Doe, John 1
Jones, Tom 4
Little, Louise 3
Smith, Sam 2
New household
Of course, it would be trivial to create the desired view if the
application included a People model, related to the Household model,
and doing so would allow for one or more "recipients" per household.
On the other hand, the forms used to create households and authorized
recipients would be more complex, or maybe you're dealing with a
legacy database having the flat file structure.
Now... is this what you wanted?
Sandy
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.