代码之家  ›  专栏  ›  技术社区  ›  Chirantan

无法在关联方法上重复nilclass

  •  3
  • Chirantan  · 技术社区  · 15 年前

    我使用的是Rails2.3.5和Ruby1.8.7。我正在建立一个简单的TODO管理器。我有属于一个用户的任务,而一个用户有许多任务。

    我在用 acts_as_taggable_on_steroids 用于标记任务和的插件 restful_authentication plugin 用于注册和用户管理。

    我收到一个奇怪的错误,在索引操作视图上显示“can't dup nilclass”。这就是控制器代码-

    @tasks = current_user.tasks
    

    当我迭代时出错 @tasks 在这个观点上。我就是这样做的 @tasks.each do |task|

    现在,当我用这个替换控制器代码时

    @tasks = Task.find(:all, :conditions => {:user_id => current_user.id})
    

    它实际上正在获取相同的记录。这只发生在开发模式中。我猜这与缓存或加载有关。

    可能有什么问题?我第一次面对这个问题。

    编辑

    好吧,这绝对是一个缓存问题。如果我做 config.cache_classes = true 在production.rb中,同样的错误也出现在production模式中。但我现在该怎么解决呢?因为我不想为我在模型/控制器中所做的每一个更改重新加载服务器。

    编辑

    下面是我的用户模型的样子

    class User < ActiveRecord::Base
      has_many :tasks
      has_many :projects
    
      # There are some validations and standard methods that resful authentication 
      # provides that I am not showing here
    
    end
    

    这就是任务模型的样子。

    class Task < ActiveRecord::Base
      belongs_to :bin
      belongs_to :project
      belongs_to :user
    
      acts_as_taggable
    
      def tag_list
        super.join(', ')
      end
    
    end
    

    任务控制器的索引方法如下

    def index
      @tasks = current_user.tasks
    
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @tasks }
      end
    end
    

    希望这有帮助。

    2 回复  |  直到 10 年前
        1
  •  5
  •   Chirantan    15 年前

    知道了。

    here ,

    一些类继承或 包含在发动机控制器中 可能无法卸载并导致 第一次请求后出现问题 你的系统。

    对我来说,那是因为我在 lib 那是猴子修补用户模型和 我想这个文件中的用户模型类没有被缓存。

    打电话 unloadable 在这个类中,lib文件夹中实现了这个技巧。所以我的lib文件看起来像这样

    class User < ActiveRecord::Base
      unloadable
      # stuff...
    end
    

    无论如何谢谢。

        2
  •  0
  •   klew    15 年前

    也许模型中的关联有问题。你能从那里粘贴一些代码吗?

    您也可以尝试在控制台中执行相同的操作。它是否给出相同的错误?查看日志,两个示例是否都生成相同的SQL查询?