간단한 사용자 모델이있는 작은 응용 프로그램이 있는데 현재 일부 소프트 삭제 기능을 추가하려고합니다 (예, 여기에 몇 가지 보석이 있음을 알고 있습니다). 사용자를 위해 잘 작동하지만 사용자를 삭제할 때 추측 된 기본 범위로 인해 삭제 된 사용자를 더 이상 찾을 수 없기 때문에 연결된 항목보기가 축소됩니다. arround를 얻는 방법에 대한 아이디어가 있으십니까?레일 소프트 삭제 has_many 연결
class User < ActiveRecord::Base
has_many :topics
has_many :comments
default_scope { where(active: true) }
end
def index
@topics=Topic.all
end
class UsersController < ApplicationController
def index
if current_user and current_user.role == "admin"
@users=User.unscoped.all
else
@users=User.all
end
end
뷰의 일부 (이 작동을 멈 춥니 다 어디 topic.user.name입니다) :
<% @topics.each do |topic| %>
<tr>
<td><%=link_to topic.title, topic %></td>
<td><%=h topic.description %></td>
<td><%= topic.user.name %></td>
<td><%=h topic.created_at.strftime('%Y %b %d %H:%M:%S') %></td>
<td><%=h topic.updated_at.strftime('%Y %b %d %H:%M:%S') %></td>
</tr>
<% end %>
컨트롤러 코드에'@topics'가 정의되어 있지 않습니까? 그 코드도 게시 할 수 있습니까? – Surya
또한 모든 항목 (활성 열이 false로 설정된 사용자의 이벤트)을 표시 하시겠습니까? – Surya
주제가 컨트롤러 인덱스를 추가했습니다. 실제로는 매우 쉽습니다. – user3133406