Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3451822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:11:13+00:00 2026-05-18T09:11:13+00:00

I have the following three models (Rails 2.3.8) class Outbreak < ActiveRecord::Base has_many :incidents,

  • 0

I have the following three models (Rails 2.3.8)

    class Outbreak < ActiveRecord::Base
        has_many :incidents, :dependent => :destroy
        has_many :locations, :through => :incidents

        accepts_nested_attributes_for :incidents, :allow_destroy => true
        accepts_nested_attributes_for :locations, :allow_destroy => true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }

    end

   class Incident < ActiveRecord::Base
        belongs_to :outbreak
        belongs_to :location
   end


   class Location < ActiveRecord::Base
 has_many :incidents
 has_many :outbreaks, :through => :incidents

     accepts_nested_attributes_for :incidents, :allow_destroy => true

   end

The parameters from the form seem to be ok

“outbreak”=>{
“locations_attributes”=>{“0″=>{“lon”=>”-1.39″, “placename”=>”wetwe”, “hpu_id”=>”15”, “postcode”=>”so1 1aa”, “region_id”=>”10”, “address_1″=>””, “town”=>”Bargate”, “address_2″=>””, “address_3″=>””, “lat”=>”50.89”}},”incidents_attributes”=>{“0″=>{“subtype_id”=>”7”, “category_id”=>”1”, “detail”=>””, “subcategory_id”=>”2”}}
}

But when the Outbreak is saved 3 rows are created in the Incidents table (the join table) and a single row in the Outbreak and Location tables.

The rows in the Incidents table are not fully populated from the params as follows:

id outbreak_id location_id category_id subcategory_id subtype_id detail  created_at    updated_at 

 57 23   NULL     1       2           7                          2010-11-25 14:45:18.385905  2010-11-25 14:45:18.385905 
 58 23   27         NULL       NULL        NULL    NULL           2010-11-25 14:45:18.39828  2010-11-25 14:45:18.39828 
 59 23   27         NULL         NULL     NULL      NULL           2010-11-25 14:45:18.403051  2010-11-25 14:45:18.403051 

This must be due to the either the format of the parameters or the multiple accepts_nested_attributes_for methods – how do I have just a single row being entered in the Incidents table with all of the parameters information?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:11:13+00:00Added an answer on May 18, 2026 at 9:11 am

    Second time so far this week I’ve answered my own question ^^ that’ll teach me to put more effort in before giving up and posting on the net for help,

    Still after looking at my original question I didn’t include enough information to answer it properly – the issue (apart from the set up of the models) was down to the Outbreak constructor in the Outbreak controller new method,

    Original Outbreaks_controller

    def new
    
        @outbreak = Outbreak.new
        @outbreak.risks.build
        //links locations directly to Outbreak instead of through Incidents
        @outbreak.locations.build
        @outbreak.incidents.build
    
        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @outbreak }
        end
    end
    

    Revised Outbreaks_controller

    def new
    
        @outbreak = Outbreak.new
        @outbreak.risks.build
        //builds Incidents then a Location through that incident
        @outbreak.incidents.build.build_location
    
        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @outbreak }
        end
    end
    

    Changes to the three models

        class Outbreak < ActiveRecord::Base
            has_many :incidents, :dependent => :destroy
            has_many :locations, :through => :incidents
    
            accepts_nested_attributes_for :incidents, :allow_destroy => true
    
    
        end
    
       class Incident < ActiveRecord::Base
            belongs_to :outbreak
            belongs_to :location
    
            accepts_nested_attributes_for :location, :allow_destroy => true
       end
    
    
       class Location < ActiveRecord::Base
           has_many :incidents
           has_many :outbreaks, :through => :incidents
    
       end
    

    This seems to work ok – also posted the create action and main form

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following models. # app/models/domain/domain_object.rb class Domain::DomainObject < ActiveRecord::Base has_many :links_from, :class_name
Hi (huge Rails newbie here), I have the following models: class Shop < ActiveRecord::Base
I have the following two models: class PhotoAlbum < ActiveRecord::Base belongs_to :project has_many :photos,
I have three classes with the following associations: class Technician < ActiveRecord::Base has_many :tickets
I have the following two models: class Product < ActiveRecord::Base belongs_to :shop validates_numericality_of :price,
I have the following model in Django: class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name=bout_fighter_1)
I have the following models: class Post(models.Model): message = models.TextField() (etc.) class UserProfile(models.Model): user
Let's say I have the three following lists A1 A2 A3 B1 B2 C1
I have the following template <h2>one</h2> <xsl:apply-templates select=one/> <h2>two</h2> <xsl:apply-templates select=two/> <h2>three</h2> <xsl:apply-templates select=three/>
I have three images and using the following code I have then fade in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.