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 8940655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:05:32+00:00 2026-06-15T11:05:32+00:00

I’m attempting use the REST API in my rails (version 3.2.9) app. I am

  • 0

I’m attempting use the REST API in my rails (version 3.2.9) app.

I am POSTing a json packet to a standard rails scaffolded controller, which includes details for the model itself, and nested children.

The parent model and its children are successfully inserted into the database. However, the response from the POST doubles the nested children.

Why are the nested associations being doubled in the POST response? I have been trying to get this right for the last few days… it is driving me nuts.

Specific examples

JSON request

POST http://localhost:3000/audio_events HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Content-Length: 432
Accept: application/json, text/plain, */*

{
    "audio_event": {
        "start_time_seconds": 0.05,
        "end_time_seconds": 15.23,
        "low_frequency_hertz": 1000,
        "high_frequency_hertz": 8753,
        "audio_event_tags_attributes": [
            {
                "tag_id": "-1"
            },
            {
                "tag_id": "-2"
            }
        ],
        "audio_recording_id": "1bd0d668-1471-4396-adc3-09ccd8fe949a"
    }
}

JSON response

HTTP/1.1 201 Created
Location: http://localhost:3000/audio_events/27
Content-Type: application/json; charset=utf-8
X-Ua-Compatible: IE=Edge
Etag: "c79ccdf981a9fadad3a8b08c3a878e8e"
Cache-Control: max-age=0, private, must-revalidate
Content-Length: 924
Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
Date: Mon, 26 Nov 2012 00:27:47 GMT
Connection: Keep-Alive

{
    "created_at": "2012-11-26T00:27:32Z",
    "creator_id": 1,
    "deleted_at": null,
    "deleter_id": null,
    "end_time_seconds": "15.23",
    "high_frequency_hertz": "8753.0",
    "id": 27,
    "is_reference": false,
    "low_frequency_hertz": "1000.0",
    "start_time_seconds": "0.05",
    "updated_at": "2012-11-26T00:27:32Z",
    "updater_id": 1,
    "audio_event_tags": [
        {
            "audio_event_id": 27,
            "created_at": "2012-11-26T00:27:32Z",
            "creator_id": 1,
            "tag_id": -1,
            "updated_at": "2012-11-26T00:27:32Z",
            "updater_id": 1
        },
        {
            "audio_event_id": 27,
            "created_at": "2012-11-26T00:27:32Z",
            "creator_id": 1,
            "tag_id": -2,
            "updated_at": "2012-11-26T00:27:32Z",
            "updater_id": 1
        },
        // DUPLICATES ARE HERE
        {
            "audio_event_id": 27,
            "created_at": "2012-11-26T00:27:32Z",
            "creator_id": 1,
            "tag_id": -1,
            "updated_at": "2012-11-26T00:27:32Z",
            "updater_id": 1
        },
        {
            "audio_event_id": 27,
            "created_at": "2012-11-26T00:27:32Z",
            "creator_id": 1,
            "tag_id": -2,
            "updated_at": "2012-11-26T00:27:32Z",
            "updater_id": 1
        }
    ],
    "audio_recording": {
        "id": 1,
        "uuid": "1bd0d668-1471-4396-adc3-09ccd8fe949a"
    }
}

Models

Many to many model

class AudioEventTag < ActiveRecord::Base
  belongs_to :audio_event
  belongs_to :tag
  accepts_nested_attributes_for :audio_event

  attr_accessible :audio_event, :tag, :tag_id

  stampable
  belongs_to :user, :class_name => 'User', :foreign_key => :creator_id

  validates_uniqueness_of :audio_event_id, :scope => :tag_id
end

The tag model

class Tag < ActiveRecord::Base
  has_many :audio_event_tags
  has_many :audio_events, :through => :audio_event_tags
  accepts_nested_attributes_for :audio_events, :audio_event_tags

  attr_accessible :is_taxanomic, :text, :type_of_tag

  stampable
  belongs_to :user, :class_name => 'User', :foreign_key => :creator_id
  acts_as_paranoid
  validates_as_paranoid
end

The audio event model

class AudioEvent < ActiveRecord::Base
  belongs_to :audio_recording

  has_many :tags, :through => :audio_event_tags, :uniq => true

  has_many :audio_event_tags
  accepts_nested_attributes_for :audio_event_tags

  attr_accessible :audio_recording_id, :end_time_seconds, :high_frequency_hertz, :is_reference,
                  :low_frequency_hertz, :start_time_seconds,
                  :tags_attributes, :audio_event_tags_attributes

  stampable
  belongs_to :user, :class_name => 'User', :foreign_key => :creator_id
  acts_as_paranoid
  validates_as_paranoid

  # validation
  validates :audio_recording, :presence => true

  validates :start_time_seconds, :presence => true, :numericality => { :greater_than_or_equal_to  => 0 }
  validates :end_time_seconds, :numericality => { :greater_than_or_equal_to  => 0 }

  validates :low_frequency_hertz, :presence => true, :numericality => { :greater_than_or_equal_to  => 0 }
  validates :high_frequency_hertz,  :numericality => { :greater_than_or_equal_to  => 0 }

  # json formatting
  def as_json(options={})
    super(
        :include =>
            [
                :audio_event_tags,
                :audio_recording  => {:only => [:id, :uuid]}
            ],
        :except => :audio_recording_id
    )
  end
end

Updates

As requested in comments, the code for my action in the controller

# POST /audio_events
# POST /audio_events.json
def create
  @audio_event = AudioEvent.new(params[:audio_event])
  #@audio_event.audio_event_tags.each{ |aet|  aet.build() }

  #@audio_event.audio_event_tags.count.times { @audio_event.audio_event_tags.build }

  respond_to do |format|
    if @audio_event.save

      format.json { render json: @audio_event, status: :created, location: @audio_event }
    else
      format.json { render json: @audio_event.errors, status: :unprocessable_entity }
    end
  end
end

And, I tried removing the stampable and acts_as_paranoid macros to no effect.

Updates 2

If I try adding 3 nested attributes, I get 6 back. 4 gives back 8, and 5 gives back 10.

  • 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-06-15T11:05:33+00:00Added an answer on June 15, 2026 at 11:05 am

    Reposting from comments.

    Looks like the json serialization issue. If separate get is working you could try @audio_event.audio_event_tags.reload before format.json in the controller.

    —
    It is certainly strange. Inmemory details for audio_event_tags got messed up at @audio_event.save line. Does AudioEvent have after_save callbacks?. If there are no callbacks then I don’t know how that can happen. Need to reproduce it. It is a serious problem to deal with if it happens for all nested attributes.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I

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.