6

내가 똑같은 스키마가 레일 대신 @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 

이 작업을 수행 할 수있는 더 레일 - 억양 방법이 있나요, 또는 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을 지정하지 않으면 Rails에는 실제로 중첩 된 속성을 허용하는 'smart_way'가 없습니다. 이 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 
+0

이 질문에 대답하지 않습니다 .. 그냥 위치뿐만 아니라, 다형성 모델의 다른 유형을 한번에 만들고 싶습니다. . – jsurf

+0

@jsurf 한꺼번에 무엇을 의미합니까? 동시에'location','checkpoint'와 함께'note'를 생성한다는 것을 의미합니까? –

+0

내 질문에 잘못 표현했습니다. 원래 한 테이블에서 ID를 참조 할 필요가 있었기 때문에 처음에는 상속 된 모델의 여러 유형으로 구성된 목록을 작성하기가 쉬운 STI 설치가있었습니다. 이제 다형성 연결로 전환 했으므로 이제 ID와 유형이 있습니다. Locations와 Checkpoint로 구성된 목록을 한 번에 만들고 싶습니다. 그래서 내가하고있는 일은 ID 배열과 배열을 사용하여 조인 테이블에 추가하는 것입니다 : ''params = [{ "notable_id"=> "9225", "notable_type"=> "위치"}, { "notable_id" => "9220", "notable_type"=> "Checkpoint"}] ''' '@note.note_joins.create params' – jsurf

관련 문제