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> Sex</b>
<b> 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>
--
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.