- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个与此处描述的完全相同的架构,其中包含一个多态连接表:http://aaronvb.com/articles/a-polymorphic-join-table.html
class Location < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
end
class Checkpoint < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
end
class NoteJoin < ActiveRecord::Base
belongs_to :notable, polymorphic: true
belongs_to :note
end
class Note < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :notable, polymorphic: true
belongs_to :user
has_many :note_joins
end
我希望能够同时创建和更新多种类型的多态关联,而不必执行 @note.locations << Location.first
或 @note.checkpoints << Checkpoint.first
.
类似于 @note.create note_joins_params
和 @note.update note_joins_params
会很棒。
到目前为止,我能够实现创建部分的方法是将属性数组传递给 @note.note_joins.create
,例如:
note_joins_params = [{"notable_id"=>"9225", "notable_type"=>"Location"}, {"notable_id"=>"9220", "notable_type"=>"Checkpoint"}]
@note.note_joins.create note_joins_params
是否有更像 Rails 的方法来完成此操作,或者是否有类似于 accepts_nested_attributes 或类似内容的适当属性哈希语法?
这也是我知道如何做 update
的唯一方法就是先删除join表中所有已经存在的记录,然后重新创建,即
@note.note_joins.destroy_all
new_note_joins_params = [{"notable_id"=>"9225", "notable_type"=>"Location"}, {"notable_id"=>"9220", "notable_type"=>"Checkpoint"}]
@note.note_joins.create new_note_joins_params
最佳答案
对于您想完成的事情,如果您不指定 source_type
,Rails 并没有真正接受嵌套属性的“智能方式”。引用这个The other side of polymorphic :through associations
但是你可以试试这个:
class Location < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
accepts_nested_attributes_for :notes
end
class Checkpoint < ActiveRecord::Base
has_many :note_joins, as: :notable
has_many :notes, through: :note_joins
accepts_nested_attributes_for :notes
end
class NoteJoin < ActiveRecord::Base
belongs_to :notable, polymorphic: true
belongs_to :note
accepts_nested_attribute_for :note
end
class Note < ActiveRecord::Base
attr_accessible :content, :user_id
belongs_to :user
has_many :note_joins
# add these, otherwise you won't be able to do nested attributes
# refer to the blog post I link above.
has_many :locations, through: :note_joins, source: :notable, source_type: 'Location'
has_many :checkpoints, through: :note_joins, source: :notable, source_type: 'Checkpoint'
end
然后在你的表单中,构建这样的东西:
# In your form
# if you want to create from the note, do this
<%= form_for @note do |f| %>
<%= f.text_field :some_note_field %>
<%= text_field_tag 'note[locations_attributes][][some_location_field]' %>
<%= text_field_tag 'note[checkpoints_attributes][]some_checkpoint_field]' %>
<% end %>
# if you want to create from the location/checkpoint, do this.
<%= form_for @location do |f| %>
<%= f.text_field :name %>
<%= text_field_tag 'location[notes_attributes][][body]' %>
<% end %>
# In your Location/Checkpoint controllers
def create
@location = Location.create(location_params)
redirect_to @location
end
def location_params
params.required(:location).permit(:name, notes_attributes: [:body])
end
# In your Note controller
def create
@note = Note.create(note_params)
redirect_to @note
end
def note_params
# the goal here is to create a set of params like this
# { note_field: 'foobar',
# locations_attributes: [{name: 'somewhere'}],
# checkpoints_attributes: [{description: 'lol'}] }
params.required(:note).permit(:note_field, locations_attributes: [:location_field], checkpoints_attributes: [:checkpoint_field])
end
关于ruby-on-rails - 一次创建多个多态记录 rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41292293/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!