getting a duplicate record with has_many and update_attributes

Please help. I've tried all i know with no results...

I have an app with a master class using has_many to another class.
When I create a new record with the params showing all fields for
master and the has_many class it seems to work OK. But doing an edit
on the record shows two copies of the has_many class, and checking
mysql shows the two copies.

class Household < ActiveRecord::Base
  has_many :people, :dependent => :destroy
  has_one :visits, :dependent => :destroy

  accepts_nested_attributes_for :people, :allow_destroy => true
  accepts_nested_attributes_for :visits

end

class Person < ActiveRecord::Base
  belongs_to :household
end

class HouseholdsController < ApplicationController
  # GET /households
  # GET /households.xml
  def index
    @households = Household.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml { render :xml => @households }
    end
  end

  # GET /households/1
  # GET /households/1.xml
  def show
    @household = Household.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml { render :xml => @household }
    end
  end

  # GET /households/new
  # GET /households/new.xml
  def new
    @household = Household.new
    @household.people.build

    respond_to do |format|
      format.html # new.html.erb
      format.xml { render :xml => @household }
    end
  end

  # GET /households/1/edit
  def edit
    @today = Date.today
    @household = Household.find(params[:id])
    @v =
Visit.find_or_create_by_household_id_and_month_and_year(:household_id
=> params[:id], :month => @today.month, :year => @today.year)
  end

  # POST /households
  # POST /households.xml
  def create
    @today = Date.today
    @household = Household.new(params[:household])
    [1,2,3,4,5,6,7,8,9,10,11,12].each do |month|
      @visit =
Visit.find_or_create_by_household_id_and_month_and_year(:household_id
=> params[:id], :month => @today.month, :year => @today.year)
    end

    respond_to do |format|
# @household.save
      if @household.update_attributes(params[:household])
        flash[:notice] = 'Household was successfully created.'
        format.html { redirect_to(@household) }
        format.xml { render :xml => @household, :status
=> :created, :location => @household }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @household.errors, :status
=> :unprocessable_entity }
      end
    end
  end

  # PUT /households/1
  # PUT /households/1.xml
  def update
       # debugger
    @today = Date.today
    @household = Household.find(params[:id])
    @v =
Visit.find_or_create_by_household_id_and_month_and_year(:household_id
=> params[:id], :month => @today.month, :year => @today.year)
    @v.update_attributes(params['visit'])
    respond_to do |format|
      if @household.update_attributes(params[:household])
        flash[:notice] = 'Household was successfully updated.'
        format.html { redirect_to(@household) }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @household.errors, :status
=> :unprocessable_entity }
      end
    end
  end

  # DELETE /households/1
  # DELETE /households/1.xml
  def destroy
    @household = Household.find(params[:id])
    @household.destroy

    respond_to do |format|
      format.html { redirect_to(households_url) }
      format.xml { head :ok }
    end
  end
end

<h1>New household</h1>

<% form_for(@household) do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
    <%= render :partial => 'people', :object => @household %>

  <%= render :partial => 'visit', :object => @visit %>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', households_path %>

<h4>People in household</h4>
  <b>&nbsp;&nbsp;Sex</b>
  <b>&nbsp;&nbsp;Birthday</b>
<table>
  <div id="people">
    <%= render :partial => 'person', :collection => @household.people
%>
  </div>
</table>
  <%= link_to_function "Add a Person" do |page|
    page.insert_html :bottom, :people, :partial => 'person', :object =>
Person.new
  end %>

<div id="person">
<% @household.build_person unless @household.people %>
<% fields_for "household[people_attributes][]", person do |
person_form| %>
<tr><td> <%= person_form.text_field :sex, :size => 1, :maxlength
=>1, :index => nil, :autocomplete => "off" %></td>
   <td><%= person_form.text_field :month, :size => 2, :maxlength
=>2, :index => nil, :autocomplete => "off" %>/
       <%= person_form.text_field :day, :size => 2, :maxlength
=>2, :index => nil, :autocomplete => "off" %>/
      <%= person_form.text_field :year, :size => 4, :maxlength
=>4, :index => nil, :autocomplete => "off" %></td>
   <td> <% unless person_form.object.new_record? %>
        <%= person_form.hidden_field :id, :index => nil %>
      <% end %>
</tr>
  <% @person = person %>
<% end %>

</div>

Please help. I've tried all i know with no results...

I have an app with a master class using has_many to another class.
When I create a new record with the params showing all fields for
master and the has_many class it seems to work OK. But doing an edit
on the record shows two copies of the has_many class, and checking
mysql shows the two copies.

You're doing this in both cases

Visit.find_or_create_by_household_id_and_month_and_year(:household_id
=> params[:id], :month => @today.month, :year => @today.year)

Which will create a new visit unless there is one with all of those
same parameters. Perhaps you only wanted
find_or_create_by_household_id ?
Also if you used the association methods (ie household.visit = etc.)
then active record would take care of destroying / nulling the
previous row

Fred

I would have many records that match only the household_id that way.
One for each month and year. I'm trying to get the record for this
month. But that part works OK. The problem is with creating people
objects. It works fine after the record is created with the correct
records being changed, added or deleted. The problem is when a record
is created. The people objects added at that time are doubled. The
only reason I didn't add visits to the models and use association
methods was that I couldn't find a way to open the correct record that
way.

Bob

this line was the problem..

@household = Household.new(params[:household])

it was saving the people records because they were linked to
household, so a later

@household.update_attributes(params[:household])

doubled the record. Changing the line to

@household = Household.new()

fixed the problem.. :>

Bob