从json POST返回的重复嵌套关联

时间:2012-11-26 01:03:43

标签: ruby-on-rails activerecord associations ruby-on-rails-3.2 has-many-through

我尝试在我的rails(版本3.2.9)应用程序中使用REST API。

我正在将json数据包发布到标准的rails scaffolded控制器,其中包含模型本身和嵌套子节点的详细信息。

父模型及其子模块已成功插入数据库。但是,POST的响应会使嵌套的子项加倍。

为什么嵌套关联在POST响应中加倍?我一直试图在过去的几天里做到这一点......这让我疯了。

具体例子

JSON请求

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

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

模型

多对多模型

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

标签模型

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

音频事件模型

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

更新

根据评论中的要求,我在控制器中执行操作的代码

# 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

并且,我尝试删除stampable和acts_as_paranoid宏以使其无效。

更新2

如果我尝试添加3个嵌套属性,我会回来6个。 4给出了8,而5给出了10。

1 个答案:

答案 0 :(得分:0)

从评论中重新发布。

看起来像json序列化问题。如果单独的get工作正常,你可以在控制器中的format.json之前尝试@audio_event.audio_event_tags.reload

- 这当然很奇怪。 audio_event_tags的内存细节在@ audio_event.save行搞砸了。 AudioEvent是否有after_save回调?如果没有回调,那么我不知道会发生什么。需要重现它。如果它适用于所有嵌套属性,那么这是一个严重的问题。