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

On Apr 12, 3:13 am, Frederick Cheung <[email protected]>
wrote:
> On 12 Apr, 05:25, Bob Smith <[email protected]> wrote:
>
> > 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
>
> >   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|
> >  # �[email protected]
> >       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>

-- 
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