我有一个小应用程序,我有一个简单的用户模型,我目前正在尝试添加一些软删除功能(是的,我知道这里有一些宝石)。对用户来说工作正常,但是当我删除一个用户时,相关的主题视图会崩溃,因为我猜想由于默认范围而无法再找到已删除的用户。 任何想法如何得到这个?视图的Rails软件与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 2014-10-30 19:28:21
你还想显示所有主题(活动列设置为false的用户的事件)? – Surya 2014-10-30 19:31:50
新增主题控制器的索引,虽然它确实很容易。 – user3133406 2014-10-30 19:54:32