我正在尝试在我的 rails(版本 3.2.9)应用程序中使用 REST API。
我正在将一个 json 数据包发布到一个标准的 rails 脚手架控制器,其中包括模型本身的详细信息和嵌套的子节点。
父模型及其子模型已成功插入数据库。但是,来自 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。