Sandy wrote:
> On Aug 25, 10:39�am, Ar Chron <[email protected]> wrote:
>>
>> Yes, that would change the landscape a bit. �Sounds like Sandy's
>> solution has the right of it by using @households to build @sorted,
>> thereby replicating the household record for each resident.
>>
>> --
>> Posted viahttp://www.ruby-forum.com/.
> 
> 
> I think that was the issue that Bob was trying to address, and I'm
> hoping it was due to his having inherited some legacy flat file
> database, and possilby not knowing how to normalize it, or being
> prevented from doing so by something else in the application which
> uses the households table.  Otherwise, the application cries out for
> relationships between a Person model (a "belongs_to :household"
> relationship) and the Household model (a "has_many :people"). While
> this does complicate, slightly, the addition of people to households,
> it would be a better way to approach the problem, and it would allow
> for more than two "authorized receivers" per household (although I
> have no idea of what the business rules are for his application, and
> they could be... 2 and only 2 per household).
> 
> Next, @sorted, as defined in the households_controller, does not
> replicate the household record for each "authorized receiver" (I
> prefer that term, rather than "resident", because a household could
> include many residents, only two of whom are authorized to receive
> food from the food bank).  What @sorted does is to provide an array
> sorted by the names of the "authorized users" with the id of the
> relevant household record.  With that id, one can use Household.find
> to obtain the relevant household record and then obtain any other
> field from that record.  Of course, if any additional fields were
> needed in the view, they could be added to the hash in the @sorted
> array when it was created in the controller, thereby avoiding the need
> to do the lookups in the view and keeping the data access logic out of
> the view.
> 
> While I did use @households in the households_controller, the only
> reason I did that was so that I could display both households and
> receivers in the same index.html.erb view, so that it was clear that
> the @sorted array was providing the "authorized users" in their sorted
> order with the id of the relevant household record.  Otherwise, better
> practice would be to have households be a local variable within the
> controller.
> 
> Sandy

Many thanks again Sandy. Another problem has appeared. The list is 
created and displayed correctly, but won't let the search function 
narrow down the list. This works perfectly using the Household variable, 
but won't narrow the search using the array created by adding both names 
together. The way I have it, the person entering the information only 
needs to get a persons license and enter the first few letters of the 
last name, narrowing the list down to a few entries that match and 
clicking the edit link next to the name wanted.

This search function, paginate, and the use of an array other than the 
Household variable don't seem to work together. I included the parts of 
controller and view that have this problem..

household_controller.rb
  def make_double_list
    @households = Household.all
    $households = Array.new
    @sorted = Array.new
 #   debugger
    for n in @households do
     @sorted << { :last_name => n.last_name, :first_name => 
n.first_name, :address => n.address, :id => n.id }
      if !n.slast_name.blank? then @sorted << { :last_name => 
n.slast_name, :first_name => n.sfirst_name, :address => n.address, :id 
=> n.id } end
    end
    $households = @sorted.sort_by { |x| x[:last_name] }
  end

  def index
    make_double_list
    if $hidden.blank? then $hidden = 0 end
  #debugger
    conditions = ["last_name like ? ", "#{params[:search]}%"]
    if $hidden == 0 then
      @households = Household.paginate :per_page => 16,
                                       :page => params[:page],
                                       :order => 'last_name',
                                       :conditions => conditions
    else
      @households = ArchivedHousehold.paginate :per_page => 16,
                                       :page => params[:page],
                                       :order => 'last_name',
                                       :conditions => conditions
      $hidden = 0
    end
  end

  def search
   # debugger
    conditions = ["last_name like ?", "#{params[:search]}%"]
    if $hidden == 0 then
      @households = Household.paginate :per_page => 16,
                                       :page => params[:page],
                                       :order => 'last_name',
                                       :conditions => conditions
      debugger
    else
      @households = ArchivedHousehold.paginate :per_page => 16,
                                       :page => params[:page],
                                       :order => 'last_name',
                                       :conditions => conditions
    end
    render :partial=>"search", :layout=>false
  end

index.html.erb
<h1>Index of Households</h1>
<%= text_field_tag 'search', params['search'], :autocomplete => "off" %>
<%= observe_field 'search',
        :frequency => 0.5,
        :update => 'searchdiv',
        :url => {:controller => 'households', :action => 'search'},
        :with => "'search=' + encodeURIComponent(value) " %>
<div id='searchdiv'>
   <%= render :partial=>"search" %>
</div>
<pre>
<% form_tag new_household_path do%>
  <%= if $hidden == 0 then submit_tag "New household" end %>
<% end %>
<%= will_paginate @households %>


_search.html.erb
<table width='75%' cellspacing=5>
  <tr>
    <th></th>
    <th width='25%' align=left>Last Name</th>
    <th width='25%' align=left>First Name</th>
    <th width='50%' align=left>Address</th>
  </tr>
<% for n in @households %>
<tr>
      <% if $hidden == 0 %>
        <td><%= link_to 'Edit', :action => 'edit', :id => n[:id] %></td>
      <% end %>
      <% if $hidden == 1 %>
          <td><%= link_to 'Restore', :action => 'restore', :id => n[:id] 
%></td>
      <% end %>
      <td><%= n[:last_name] %></td>
      <td><%= n[:first_name] %></td>
      <td><%= n[:address] %></td>
      <td></td>
      <% if $hidden == 0 %>
        <td><%= link_to 'Delete', :action => 'delete', :id => n[:id], 
:confirm => 'Are you sure ?' %></td>
      <% end %>
    </tr>
  <% end %>
</table>
-- 
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 [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.

Reply via email to