मैं के रूप में एक बहुरूपी में शामिल होने की मेज के साथ यहाँ वर्णित एक ही सटीक स्कीमा है: 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
वहाँ एक और अधिक रेल-esque तरीका यह है, उचित गुण हैश वाक्य रचना 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
रेल बहुरूपी संघ के लिए accept_nested_attribute के लिए समर्थन की जरूरत नहीं है। हो सकता है कि आपको यह थ्रेड http://stackoverflow.com/q/3969025/4587148 – Sajan