Unable to duplicate NilClass association methods

I use rails 2.3.5 and ruby ​​1.8.7. I am creating a simple TODO manager. Where I have tasks belonging to the user, and the user has many tasks.

I use the acts_as_taggable_on_steroids plugin for tag tasks and the restful_authentication plugin for registering and managing users.

I get a strange error that reads "Unable to duplicate NilClass" in the index action view. This is the controller code -

 @tasks = current_user.tasks 

The error occurs when I repeat over @tasks in the view. That is, when I do @tasks.each do |task|

Now when I replace the controller code with this

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

In fact, you get the same records. This only happens in development mode. I guess this has something to do with caching or loading.

What could be wrong? I encountered this problem for the first time.

EDIT

Well, this is definitely a caching issue. If I make config.cache_classes = true in production.rb, the same error occurs in production mode. But how do I fix this? Because I do not want to restart the server for every change I make in models / controllers.

EDIT

This is what my User model looks like

 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 

And this is what the task model looks like.

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

The task controller index method is as follows:

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

Hope this helps.

+3
source share
2 answers

Got it.

From here

Some of the classes inherited or included in your engine controllers may not unload and cause a problem after the first request of your system.

For me, this was because I had a file in lib that was the monkey of the User model patches and I believe that the user model class in this file was not cached.

Calling unloadable in this class in the lib folder did the trick. So my lib file looks like this:

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

Thanks anyway.

+5
source

Perhaps something is wrong with the associations in the model. Can you paste the code from there?

You can also try to do the same in the console. Does this give the same error? Look in the logs, both examples generate the same SQL query?

0
source

Source: https://habr.com/ru/post/1316124/


All Articles