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
New household
<% form_for(@household) do |f| %>
<%= f.error_messages %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= render :partial => ‘visit’, :object => @visit %>
<%= f.submit 'Create' %>
<% end %><%= link_to ‘Back’, households_path %>