Rails 4.1 with ActiveJob gem - uninitialized constant ActiveJob - ruby-on-rails

I have followed the getting started with active job article by EngineYard. The article states:
You'll need Rails 4.2.0beta1 or greater if you want to Active Job available by default (in older versions of Rails, you can require it as a gem)
I am trying to use ActiveJob in my Rails 4.1 project. I added ActiveJob to my gemfile gem 'activejob'. As per the article, I have:
#config/initializers/active_job.rb
ActiveJob::Base.queue_adapter = :resque
However, when I run rails server I get the following error:
config/initializers/active_job.rb:1:in': uninitialized constant ActiveJob (NameError)`
EDIT - Fixed typo "gem active job"
UPDATE 1
Following solydest's suggestion below, adding require 'active_job' to application.rb allows me to no longer get the uninitialized constant ActiveJob error but instead I receive the error undefined method perform_later' when I try to call my job. I am following the edge rails guide and enqueue my job with code similar to:
MyJob.perform_later(record)
The beginning of my job class:
class MyJob < ActiveJob::Base
queue_as :images
def perform(id)

I added require 'active_job' to config/application.rb just below all the other require directives and that solved the issue for me.

I've found that the gem version of ActiveJob for rails 4.1 is 0 which is different from the version in Rails 4.2.
If you're using the v0 of ActiveJob, the syntax should be :
MyJob.enqueue(record)
or
MyJob.enqueue(record, options)
You will find a lot of interesting things about ActiveJob with Rails 4.1 in this article :
http://kinopyo.com/blog/use-activejob-in-rails-4-1/

Use this gem if you want to use Active Job on Rails 4.0 or 4.1:
https://github.com/ankane/activejob_backport
But this gem did not pull Action Mailer in, you need to stick with deliver, see https://github.com/ankane/activejob_backport/issues/1.

Related

Updating from Rails 3.2.0 to 3.2.8 caused uninitialized constant Addressable

In a project I was given a task to update all gems to stable. We were using rails 3.2.0 and some other gems. After updating to version 3.2.8 all tests crushed and I wa to fix them, the last error is ActionView::Template::Error: uninitialized constant Addressable. I have a file with
module URIHelpers
class << self
...
def parse_url(url)
Addressable.URI.heuristic_parse(url)
end
...
end
end
When calling URIHelpers.parse_url(url) I'm getting the above error. Where could be a error?
I've got an answer by myself :) I was to add
require 'addressable/uri'
at the begging of the file. Before the update it was required somewhere in dependent gems and after it was out and i needed to add it to gemfile by myself and require it by hand.

Rails sitemap_generator Uninitialized Constant?

I'm trying to use the Rails site map_generator gem to generate site maps for a 8,000,00 page site. The gem can be found here: https://github.com/kjvarga/sitemap_generator
Here is my code in sitemap.rb:
require 'rubygems'
require 'sitemap_generator'
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "http://www.mysite.com"
SitemapGenerator::Sitemap.create do
add '/content.aspx?page=privacypolicy'
Product.find_each do |product|
add product_path(ppid), :lastmod => content.updated_at
end
end
However, when I run
>> ruby sitemap.rb
I get an error that says:
sitemap.rb:9:in `block in ': uninitialized constant
SitemapGenerator::Interpreter::Product (NameError)
However "Product" is the correct name of my model. Why is this happening?
I'm running Rails 3.1.2 and Ruby 1.9.
I'm the author of the gem. Better to open an issue on the GitHub page in future. SitemapGenerator does work in Rails 3 and Ruby 1.9.*. If you are running Rails, you don't need these lines:
require 'rubygems'
require 'sitemap_generator'
Also you generate your sitemaps by running Rake:
rake sitemap:refresh:no_ping
What is happening in your case is that because you're not running through Rake, the script does not know about the Product class, since your Rails environment hasn't been loaded.
Well, I wasn't able to get this gem working. My guess is that it doesn't work on Rails 3.1.2 or with Ruby 1.9. However, I was able to get another gem (big_sitemap) to work. Here is the link to it.
https://github.com/alexrabarts/big_sitemap

CanCan ruby on rails error

I'm just starting a new project in Rails and I wanted to add some CanCan roles checking to app. What have I done:
1. Installed proper gem
2. Included in gemfile "cancan"
3. Created ability class
4. In my ApplicationController - added "check_authorization"
But still, I get
undefined local variable or method `authorize_resource' for ApplicationController:Class
or
undefined local variable or method `check_authorization' for ApplicationController:Class
error.
Somebody got any ideas what can be the reason why?
My cancan version is 1.6.7.
Gem version is 1.9.1
As the author #Animattronic said, restart your Rails server.

const_missing error when changing I18n backedn

I'm trying to upgrade the I18n backend of my application to use the database instead of the yml files for internationalization. I'm following the steps found for the I18n-active_record gem found here: https://github.com/svenfuchs/i18n-active_record.
Unfortunately, the aws-s3 gem seems to be conflicting somehow as I can't even start my server or console once I create the locale.rb initializer. Here is a summary of the steps I'm following:
gem "i18n-active_record", "~> 0.0.2"
create new file config/initializers/locale.rb
inside locale.rb
I18n.backend = I18n::Backend::Database.new
restart localhost server to load initializer
Error Message
/Users/user_name/.rvm/gems/ree-1.8.7-2010.02#app/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing': uninitialized constant I18n::Backend::Database (NameError)
...(Several more lines)
Any help or insight would be appreciated!
Great question and great discussion. The answer is contained in a combination of the comments up above, but for those of you who are upgrading to a Rails 3 application, this is a summary of the steps I had to take.
Add this to your Gemfile:
gem 'i18n-active_record', :require => 'i18n/active_record'
Add this to a new config file config/initializers/locale.rb
require 'i18n/backend/active_record'
I18n.backend = I18n::Backend::ActiveRecord.new
Take out any code in application.rb that was previously initializing the record store. This clears the missing content errors. Full instructions are on the Github repo for this backend module which was removed from the core I18n: https://github.com/svenfuchs/i18n-active_record
Although the readme in github says so, I don't think the Database constant is actually defined by the gem. Try
I18n.backend = I18n::Backend::ActiveRecord.new

uninitialized constant Delayed::Job

I've added the delayed_job gem to my gemfile and installed correctly but when I try to run the following line:
Delayed::Job.enqueue do_it(), 0, 1.minutes.from_now.getutc
I get the error 'uninitialized constant Delayed::Job'
Can somebody explain what i need to do here? I've tried running 'rake jobs:work' beforehand but it also returns the 'uninitialized constant Delayed::Job' error. Additionally, I've added "require 'delayed_job'" to the file (application.rb) without much luck.
If you've upgraded to delayed_job version >=3 you'll need to add this (presuming you're using ActiveRecord):
# Gemfile
gem 'delayed_job_active_record'
Did you follow the installation instructions on the README file? https://github.com/collectiveidea/delayed_job
Add this to your gemfile:
gem 'delayed_job_active_record'
and then run this at the console:
$ rails generate delayed_job:active_record
$ rake db:migrate
You need to create the delayed jobs table in the database (this assumes you're using active record).
For Rails 3, all you need to do is include it in the gemfile, run that code above to create the table and migrate the database, then restart your server and go!
I'm using delayed job within an engine (so the gem is specified in a .gemspec rather than Gemfile) and was getting the same error. I found that I could solve the problem by using:
require 'delayed_job_active_record' # fixes problem
rather than
require 'delayed_job' # doesn't
Just in case, if this is still unanswered, check the below link
http://www.pipetodevnull.com/past/2010/4/14/uninitialized_constant_delayedjob/
edit: Alternative, just upgrade to the latest version - 2.1
i was struggling a while back with the same problem. i was following ryan bates screencast on delayed_job and got the same error 'uninitialized constant Delayed::Job'. In the screencast ryan creates a file called mailing_job.rb(located under lib folder) with the delayed_job perform method inside, which allows you to use the enqueue method. After doing some research i found that rails 3 does not automatically load the lib folder files into your app.(not entirely sure)
Try this
In your controller where you use this:
"Delayed::Job.enqueue do_it(), 0, 1.minutes.from_now.getutc"
Try to require the file like this.
require 'mailing_job'
class AssetsController < ApplicationController
def some_method
Delayed::Job.enqueue do_it(), 0, 1.minutes.from_now.getutc
end
end
Version change possibility : if you jump from the 2.1.x to the 3.x version of the gem via a non locked down bundle, you may have a similar issue.

Resources