比较控制器内两个不同模型的属性

时间:2011-10-28 11:04:12

标签: ruby-on-rails ruby ruby-on-rails-3

我想将@question.id与数据库表Work中的值进行比较。我怎么能在问题控制器的show动作中做到这一点?

我想将@question.id@community.community_activity进行比较,但当我在问题控制器中执行此操作时,@communitynil

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.community_activity

如何解决这个问题?

if @question.id != @community.community_activity.for_communities(60).object_id and current_user.role.title.strip == "Test" 
  redirect_to 'public/access_denied.html'
  return

1 个答案:

答案 0 :(得分:1)

根据您向我们展示的内容,我所能提出的建议如下:

if @community.nil? || (@question.id != @community.community_activity.for_communities(60).id && current_user.role.title.strip == "Test")
  redirect_to '/access_denied.html'
  return
end

或许这更合适:

if (@community.nil? || @question.id != @community.community_activity.for_communities(60).id) && current_user.role.title.strip == "Test"
  redirect_to '/access_denied.html'
  return
end
相关问题