Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-plugins

Rails 3 plugin - how to access app's model file in require statement?


I am writing a plugin in Rails 3.2.6 - my plugin is supposed to modify my User model (located in app/models/user.rb) - however, I cannot find the proper way to reference that file from the plugin in a require statement.

I tried require 'user', require 'app/models/user', and a bunch of stuff trying to use Rails.root and other variables - nothing works and I get an error "Uninitialized constant User" when trying to run the app.

Can someone please point me in the right direction? BTW, this plugin was build as a gem and is included in my Gemfile (which pulls it from github).

Thanks!


Solution

  • If you are on ruby 1.9, you should be able to use require_relative

    #assuming /lib/yourfile.rb
    require_relative '../app/models/user.rb'
    

    If you are on an older ruby, you might try

    require File.dirname(__FILE__) + '../app/models/user.rb'