[英]Creating multiple polymorphic records at once rails
我有與此處描述的完全模式相同的多態連接表: 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
的唯一方法是首先刪除連接表中的所有現有記錄,然后重新創建它們,即
@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實際上沒有接受嵌套屬性的'smart_way'。 參考這個多態的另一面:通過關聯
但你可以嘗試這個:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.