User does not respond to ‘devise’ method

I’ve recently went back to an old project and it seems to have stopped working giving me this error:
~/projectx/vendor/ruby/1.9.1/gems/devise-2.0.0/lib/devise/rails/routes.rb:391:in `raise_no_devise_method_error!': User does not respond to 'devise' method. This usually means you haven't loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb' (RuntimeError)
[read full stack trace at: gist]
Since I didn’t really change anything, I was surprised to see it break. So I tried to do what it said, I changed that I had a require statement for my ORM which was Mongoid. I had: (in config/devise.rb)
Devise.setup do |config|
# ==> ORM configuration
# Load and configure the ORM. Supports :active_record (default) and
# :mongoid (bson_ext recommended) by default. Other ORMs may be
# available as additional gems.
require 'devise/orm/mongoid'
# ... more settings ...
end

But that wasn’t working, so I tried to move it to my config/application.rb before the definition:
# Pick the frameworks you want:
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
require 'devise/orm/mongoid'

Doing that it just produced:
~/projectx/vendor/ruby/1.9.1/gems/orm_adapter-0.0.6/lib/orm_adapter/adapters/mongoid.rb:6:in `<module:ClassMethods>': uninitialized constant Mongoid::Document::ClassMethods::OrmAdapter (NameError)
[read full stack trace at: gist]

So that wasn’t getting me anywhere.

Since really neither of the errors resulted in any information that could help me, I started looking at what I can do to maybe change the issue. I tried updating the gems like devise and mongoid but that didn’t do anything. Now I wouldn’t recommend people do this but I decided to update my app from Rails 3.0 to Rails 3.2, hoping the issue gets solved with that, plus I knew this app was slated to be updated to 3.2 in the near future anyways. So I did that and it didn’t help at all. I walked away from the app for a few days because I knew pulling my hair out wouldn’t get me anywhere.

Today I looked at the app again and still had no idea what was wrong, but I looked at my Gemfile and noticed that I had  some unneeded gems for Rails 3.2. Gems like jammit, rails-3-generators and active_reload. After running ‘bundle install’ the app booted up without errors! Reinstalled the gems one by one I found out that active_reload was the reason it was happening.

If you’re getting of the errors that I was seeing and you’ve ensured that everything is setup correctly, try removing active_reload and see if that helps!

Learn More

Wicked_PDF working in Rails 3.1

I recently updated one of my apps to Rails 3.1 and found that my PDF generating tests were failing because they were unable to find the CSS or images to use in the PDF.

So I started playing around, and here is what I got:

In your app, add a new initializer (or edit your existing wicked_pdf.rb initializer) and put the following:

As you can see in the code, I also replaced the background urls with correct references, this is assuming that you’re keeping your CSS images in the same location as your normal images (which I don’t see a reason why not but of course you can always tweak the script)

Possible Issues

Pow.cx

I had one issue with my generated PDF using pow.cx, my CPU would jump to 100% (on the pow process) and the page would timeout. After upgrading to the latest version of pow (0.3.2) using this command:

curl get.pow.cx | sh

I was able to see my PDFs without an issue.

Additional Sources:

http://blog.phusion.nl/2011/08/14/rendering-rails-3-1-assets-to-string/
https://github.com/mileszs/wicked_pdf/issues/48

Small update: (14/02/2012)

Small change to the above code to fix issue that prevented CSS from being rendered correctly.

Learn More

MongoMapper – undefined method ‘reflect_on_association’

Information Tidbit:

If you’re using reflection_on_association with ActiveRecord and need to use it with MongoMapper, here is what you want to do:

undefined method `reflect_on_association' for User:Class

ActiveRecord:
User.reflect_on_association(:posts).klass.new

MongoMapper:
User.associations[:posts].klass.new

Mongoid:

User.associations['posts'].klass.new
notice the posts is a string, it will not work with a symbol

UPDATE – (Apr 2011) Mongoid now follows ActiveRecord’s style naming:

User.reflect_on_association(:posts).klass.new

Learn More

Getting Paperclip to work with MongoMapper

Last night I was trying really hard to get MongoMapper to work with Paperclip via GridFS and failed. It seems right now Paperclip or even CarrierWave are not ready to be used with the latest MongoMapper drivers and GridFS (and I needed to use the latest drivers due to Scopify).

After giving up using GridFS, I wanted to use Paperclip with MongoMapper with the normal file system and I found this article from Ben Curtis, however with the latest version of Paperclip (version 2.3.1.1) and MongoMapper (version 0.7.6) I was unsuccessful, kept getting:

Anonymous modules have no name to be referenced by

After reading the backtrace and diving into the paperclip source code I noticed that it was trying to use the ActiveRecord.logger and since I’ve removed ActiveRecord from my app, it was failing (with the odd error).

After some tweaking of Ben’s code, I came up with this: http://gist.github.com/424158

Learn More

MongoMapper logging to Rails log

I started playing around with MongoMapper for MongoDB and I have to say I’m loving it! Somehow it just feels so much easier to work with. However, I did have an issue with my code and needed to see whether or not MongoMapper was looking for account_id in my model. Since there is no sql to look at, I was looking for a way to see what MongoMapper was sending to MongoDB. Luckily the answer was very simple, in your connection string you just have to do this:

MongoMapper.connection = Mongo::Connection.new('localhost', 27017, :logger => Rails.logger)

This little tidbit came from Zyph blog post

Learn More

uninitialized constant Rails::Railtie

A few days ago my app just stopped working, after a quick google search I found a great site that explains why:

Jamie van Dyke that pointed out that inherited_resources has now fully moved to Rails 3 and will no longer work on Rails 2.3 or lower. Now Jamie seems to point to use older versions of the gem because they changed the code for Rails 3.0, however I feel that his versions aren’t the latest version you could be running. So I wanted to add my 2 cents by showing you what I use:

In my environment.rb make sure you have both these lines in:
config.gem "inherited_resources", :version => "1.0.2"
config.gem "responders", :version => "0.4.2"

Hope that helps some of you out!

Happy coding!

Learn More