Project

General

Profile

Extending core model by adding patch code to the init.rb - Only works in first query !

Added by Miguel Akira over 12 years ago

Hello everyone!

I'm trying to extend the core User model to add a "has_and_belongs_to_many :categories". I added on the init.rb of my plugin:

#init.rb

require 'redmine'
require 'user'

#regular init.rb stuff...

module UserPatch
  def self.included(base)
    # Same as typing in the class 
    base.class_eval do
      unloadable # Send unloadable so it will not be unloaded in development
      has_and_belongs_to_many :categories

    end
  end
end

User.send(:include, UserPatch)

And in my view, for example, I use:
users.each do |user|
  user.categories
end

In the first time I run the code, it works well, and I get all the categories of the user. But if I refresh the page, or go back and forth, or do anything that will make the code run again, it won't work - it will say that there's no method 'categories' for the user object.

It's as if the line 'has_and_belongs_to_many :categories' gets deleted after the first run.

What could be the problem here?

Thanks!


Replies (1)

RE: Extending core model by adding patch code to the init.rb - Only works in first query ! - Added by Miguel Akira about 12 years ago

Well I don't know exactly what was happening, but if I remove unloadable from base.class_eval, then it works, albeit only in production mode!

Thanks!

    (1-1/1)