2016-08-17 1 views
1

레일 3.2에서 레일 5로 업그레이드 할 때 갑자기 모델 저장이 거부되는이 문제가 발생했습니다.레일 5 _ids 배열 할당이 has_many : through associations에서

ClosingDayLocation에는 has_many : through 관계가있는 ClosingDayLocation 모델이 있습니다. ClosingDay을 만들 때 사용자는 확인란을 사용하여 하나 이상의 Location 레코드를 선택해야합니다. 내가 양식에 선택 Location 기록, 난 항상 오류가 closing_day_id가 비어 있기 때문에 관련 ClosingDayLocation이 무효라고 나에게 알려주는 ClosingDay 기록을 저장할 수에 관계없이 어느 그러나

.

모델 :

class ClosingDay < ActiveRecord::Base 
    has_many :closing_day_locations, :dependent => :destroy 
    has_many :locations, :through => :closing_day_locations 
end 

class Location < ActiveRecord::Base 
    has_many :closing_day_locations, :dependent => :destroy 
    has_many :closing_days, :through => :closing_day_locations 
end 

class ClosingDayLocation < ActiveRecord::Base 
    belongs_to :location 
    belongs_to :closing_day 

    validates :closing_day_id, :presence => true 
    validates :location_id, :presence => true 
end 

컨트롤러

<%= form_for(@closing_day) do |f| %> 
    <%= show_errors_for(@closing_day) %> 
    <table> 
     <tr> 
     <td><%= f.label :name %></td> 
     <td><%= f.text_field :name %></td> 
     </tr> 
     <tr> 
     <td><%= f.label :date %></td> 
     <td><%= f.text_field :date %></td> 
     </tr> 
     <tr> 
     <td><%= label_tag 'closing_day[location_ids][]', 'Locations' %></td> 
     <td> 
      <%= hidden_field_tag 'closing_day[location_ids][]', nil %> 
      <% @locations.each do |location| %> 
       <p> 
       <%= label_tag do %> 
        <%= check_box_tag 'closing_day[location_ids][]', location.id, @closing_day.location_ids.include?(location.id) %> 
        <%= location.name %> 
       <% end %> 
       </p> 
      <% end %> 
     </td> 
     </tr> 
     <tr> 
     <td colspan="2"><%= f.submit %></td> 
     </tr> 
    </table> 
<% end %> 

그래서 짧은

def create 
    @closing_day = ClosingDay.build(closing_day_params) 

    if @closing_day.save 
     redirect_to(closing_days_url, :notice => 'OK') 
    else 
     @locations = Location.active.order(:name) 
     render :action => 'new' 
    end 
    end 

    private 
    def closing_day_params 
    params.require(:closing_day).permit(:date, :name, location_ids: []) 
    end 

양식, 난을 만들려면 새로운 @closing_day을 기록하고 본질적으로 @closing_day.location_ids = [3,6,9]을 사용하여 Location ID를 할당하여 관련 (기존) 위치를 할당하십시오. 이것은 Rails 3.2에서 작동하지만 Rails 5에서는 작동하지 않는 것 같습니다.

이 기능을 계속 유지하는 가장 좋은 방법은 무엇입니까? 나는 또한 accepts_nested_attributes_for :locations, :allow_destroy => trueaccepts_nested_attributes_for :closing_day_locations, :allow_destroy => true을 추가하려고 시도했지만 작동하지 않는 것 같습니다. 연결된 ClosingDayLocation 모델이 생성 된 것처럼 보이지만 closing_day_id은 비어있어 오류가 발생합니다.

답변

0

좀 더 땜질 한 후에 나는 문제를 발견하고 해결했습니다. 다른 사람들이 같은 문제에 부딪 힐 경우에 대비하여 여기에서 공유 할 것입니다!

class ClosingDayLocation < ActiveRecord::Base 
    belongs_to :location 
    belongs_to :closing_day 

    #validates :closing_day_id, :presence => true 
    #validates :location_id, :presence => true 
end 

중간 모델에서 이러한 2 가지 유효성 검사를 제거한 후에는 생성 및 업데이트시 모두 정상적으로 작동했습니다. 분명히 관련 레코드의 유효성 검사 및 저장에 대한 일부 주문은 Rails 3.2와 Rails 5 사이에서 이동했습니다.

+1

또한 has-and-belong-to-many를 사용했지만 코드에서 has_many : through를 사용한다고 생각했습니다. –

+0

수정. 나는 갱신 할 것이다! 감사. – ChrisDekker