6

मैं के रूप में एक बहुरूपी में शामिल होने की मेज के साथ यहाँ वर्णित एक ही सटीक स्कीमा है: 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 
+0

रेल बहुरूपी संघ के लिए accept_nested_attribute के लिए समर्थन की जरूरत नहीं है। हो सकता है कि आपको यह थ्रेड http://stackoverflow.com/q/3969025/4587148 – Sajan

उत्तर

6

आप क्या हासिल करना चाहते हैं के लिए, यदि आप source_type निर्दिष्ट नहीं करते हैं तो रेलों में वास्तव में नेस्टेड विशेषताओं को स्वीकार करने का यह '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 

फिर कुछ इस तरह का निर्माण: इस The other side of polymorphic :through associations

का संदर्भ लें लेकिन आप इस कोशिश कर सकते हैं

# 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 
+0

जांचना चाहिए, यह मेरे द्वारा पूछे गए प्रश्न का उत्तर नहीं देता है .. मैं सिर्फ एक स्थान नहीं बनाना चाहता हूं, लेकिन विभिन्न प्रकार के पॉलीमोर्फिक मॉडल एक बार में बनाना चाहता हूं । – jsurf

+0

@jsurf आप सभी का एक बार में क्या मतलब है? क्या आपका मतलब 'स्थान', 'चेकपॉइंट 'के साथ एक ही समय में' नोट 'बनाना है? –

+0

मैंने अपने प्रश्न को गलत तरीके से phrased किया। मैं मूल रूप से एक एसटीआई सेटअप था जहां एक बार में विभिन्न प्रकार के विरासत मॉडल से बना एक सूची बनाना आसान था, क्योंकि मुझे केवल एक तालिका से आईडी को संदर्भित करने की आवश्यकता थी। अब जब मैंने पॉलिमॉर्फिक एसोसिएशन में स्विच किया है, तो अब मेरे पास आईडी और टाइप है। मैं एक ही समय में स्थान और चेकपॉइंट दोनों से बना एक सूची बनाना चाहता हूं। तो मैं जो कर रहा हूं वह आईडी की सरणी का उपयोग करके जॉइन टेबल में जोड़ रहा है और टाइप: '' params = [{"notable_id" => "9225", "notable_type" => "स्थान"}, {"notable_id" => "9220", "notable_type" => "चेकपॉइंट"}] '' '@note.note_joins.create पैराम्स – jsurf

संबंधित मुद्दे