On Aug 25, 6:04 am, Sandy <[email protected]> wrote:
> 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
In the interest of completely responding to your question about
getting the "Edit" links, and to simplify the view code, you may do
the sorting in the household_controller. Then, you would simply
iterate over the (already) sorted @sorted array. The only other
"trick" is that you do have to use the find function in the view, as
the restful routing is based on the household object.
Finally, in my penultimate paragraph, I meant Person (not People)
model.
That being said, here is the revised code:
Controller:
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
@sorted = @sorted.sort_by { |n| n[:aname] }
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @households }
end
end
View:
<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>Recipients</h1>
<table>
<tr>
<th>Name</th><th>Household ID</th>
</tr>
<% @sorted.each do |n| %>
<tr>
<td><%= n[:aname] %></td>
<td><%= n[:id] %>
<td><%= link_to 'Show', Household.find(n[:id]) %></td>
<td><%= link_to 'Edit',
edit_household_path(Household.find(n[:id])) %></td>
<td><%= link_to 'Destroy', Household.find(n[:id]), :confirm =>
'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br/>
<%= link_to 'New household', new_household_path %>
Now, the Show, Edit, and Destroy links are shown with each Recipient
in the list of Recipients, but they are routed to the associated Show,
Edit, and Destroy functions in the household_controller. Again, I have
shown the id from the households table in both the Households Listing
and the Recipients listing so that it's easier to see what's going on.
You will probably need only the Recipients listing portion of
index.html.erb, but the way I coded it, you can easily see that it
works. As always, I test code before posting it here.
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.