uninitialized constant - loading class issue? - ruby-on-rails

So, I got mailer app/mailers/dynamic_mailer.rb and model app/models/email_message/outgoing.rb. There is method:
class EmailMessage::Outgoing < EmailMessage
...
def deliver_mail
l = ::DynamicMailer.email_message(self).deliver!
Rails.logger.info "SEND MAIL: #{l.inspect}"
update_attribute(:received_at, Time.now)
end
Locally (developement env) everything works fine. The problem occurs when I'm deploying app to server (staging env) and trying to send email form there. Delayed job prints:
[Worker(host:rdev pid:2279)] EmailMessage::Outgoing#send_email!
failed with NameError: uninitialized constant
EmailMessage::Outgoing::DynamicMailer - 11 failed attempts
It looks like a problem with loading classes on server. Removing double colons before class name fails.
Any help will be greatly appreciated.

Try to specify file with DynamicMailer obviously in file with your model like this require 'app/mailers/dynamic_mailer.rb'. Probably it can help to find necessary class.
Also I've noticed that in error message is mentioned send_email! method but you posted here def deliver_mail method. Whether I don't understand something or you're looking in wrong place.

I just forgot to restart delayed job daemon.
You can do it with capistrano, by adding gem 'daemons' to your Gemfile and updating receipes like that: http://cmar.me/2011/02/21/delayed_job-with-rails-3-and-capistrano/

Related

Conflict between Rails Admin and Impressionist gems

I'm using the latest version of the Impressionist and Rails Admin gems, and wondering if anyone could shed some light on an annoying conflict I'm experiencing. The problem is roughly documented here - https://github.com/sferik/rails_admin/issues/1315, yet the vaguely described solution is not working for me. When I have the line is_impressionable in my Listing model, I get an error when starting my Rails server with rails s:
...rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined local variable or method `is_impressionable' for Listing(no database connection):Class (NameError)
If I first start the server, and then add the 'is_impressionable' line, everything works fine, so the problem only occurs during initialization. I don't fully understand the initialization process, so am not sure how to go about getting this to work.
I have tried moving all my rails_admin model configuration options to their respective models, rather than in the initializer, which had no effect. I also have the following line in my initializer:
config.included_models = [Listing,ListingImage,AllOtherModelsHere...]
I have tried adding single quotes around these model names, which results in the following errors, as described in the github issue here
[RailsAdmin] Could not load model Listing, assuming model is non existing. (undefined local variable or method `is_impressionable' for Listing(no database connection):Class)
Any ideas what else I can try to make these gems work together? I don't want to have to remove the is_impressionable line every time I want to restart the server or generate a migration...
Not sure if the same issue that I had but yet I will post what worked for me just in case someone struggles with this too:
Im on a ruby 2.1.5 project with rails 4.2.0 and among other gems I'm using rails admin.
I run into several weird problems trying to set this up. For instance if I added the is_impressionable call within one of my models for some reason the execution of that file stopped there and I started getting weird errors like any method declared below the is_impressionable failed with undefined error.
So what I end up doing was:
class MyModel < ActiveRecord::Base
include Impressionist::IsImpressionable
is_impressionable
end
So this solved my issue and now i can access #my_model_instance.impression_count as expected.
I changed every occurrence of Klass to 'Klass'.constantize in initializer.

Specifying the host in Rails performance tests

I have been trying to use the Rails profiling tools. I am using a very simple example taken from the docs at http://guides.rubyonrails.org/performance_testing.html that looks like this
require 'test_helper'
require 'rails/performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionDispatch::PerformanceTest
def test_homepage
get '/'
end
end
I then run the test using
rake test:profile
but it crashes with the following error
Error during failsafe response: undefined method `controller_name' for nil:NilClass
I suspect that the problem is that the app serves multiple domains and so simply using get '/' is not enough information to resolve the url back to a controller/action - it needs a host as well. However the usual ways of specifying a host (#host, #request.host, default_url_options[:host]) either don't work or cause another error (eg #request is nil).
I have also tried entering the full url. I have the different hosts defined as constants in the test environment so this looked something like
get "http://#{HOST_1}/"
In this case the rake task completed successfully but no profiling information appeared on the command line and no files were generated.
I haven't really used the profiling tools in Rails much so I am hoping I am missing something obvious. Any pointers would be much appreciated.
Cheers

rails resque and resque-send-later plugins causing "unitialized constant ClassName::Resque" error

i'm using the resque and resque-send-later PLUGINS (not gems) in my project.
I haven't put 'require' statements anywhere in the code at all (since they're plugins and so they must be included upon initialization).
the app is working perfectly locally, but on heroku, it shows an error
"const_missing: unitialized constant User::Resque"
my User model:
class User < ActiveRecord::Base
include Resque::Plugins::SendLater
def self.testingWorker1
# code to be run in the background
end
end
my User_controller: (where i'm calling the above method from)
class UserController < ApplicationController
def testingResqueWorker
User.send_later(:testingWorker1)
end
end
so I removed the line include Resque::Plugins::SendLater from the my model
it still works perfectly locally, but now on heroku it gives an error saying "method_missing: send_later"
my question is:
1. how do we 'include' or 'require' plugins in rails? are they automatically available to all controllers and models?
2. any ideas for how to fix the above errors?
Two thoughts
any reason why you aren't using the gems?
are you sure the plugins have been added to the git repository and therefore have been deployed to heroku?

First Call to a Controller, Constant is defined, Second call, "uninitialized constant Oauth"?

I am trying to get the OAuth gem to work with Rails 3 and I'm running into this weird problem... (independent of the gem, I think I've run into this once before)
I have a controller called "OauthTestController", and a model called "ConsumerToken". The model looks like this.
require 'oauth/models/consumers/token'
class ConsumerToken < ActiveRecord::Base
include Oauth::Models::Consumers::Token
end
When I go to "/oauth_test/twitter", it loads the Oauth::Models::Consumers::Token module and I'm able to connect to twitter no problem. But the second time I try it (just refresh the /oauth_test/twitter url), it gives me this error:
NameError (uninitialized constant Oauth):
app/models/consumer_token.rb:4
app/models/twitter_token.rb:2
app/controllers/oauth_test_controller.rb:66:in `load_consumer'
Why is that? It has something to do with load paths or being in development mode maybe?
Try using require_or_load instead of require. That forces full load each time when in development and can sometimes help with this sort of issue.
Yeah it's something to do with being in development mode. Setting config.cache_classes = true in your development.rb get's it working (but is a pain in the ass)

How do I use a custom log for my rake tasks in Ruby on Rails?

I have a rake task that calls functions like this:
namespace :blah do
task :hello_world => :environment do
logger.info("Hello World")
helloworld2
end
end
def helloworld2
logger.info("Hello Again, World")
end
I want the log output to a custom log, and I really don't want to have to pass a log reference every time I make a function call. I found this somewhere (can't find it again):
def logger
##logger ||= Logger.new("#{RAILS_HOME}/log/blah.log")
end
But this does not work for me and I am not sure what it even does because I grabbed the code a long time ago and haven't used it until now. I can't search for ## on google (tried +"##" rails) to see what it does. Any help on this issue would be great. I am hoping for a quick solution and not having to install a gem or plugin (unless there is a really really good reason to.
Thanks!
rake disables logging in production mode. make sure you're running in development mode if you want it to log
What do you mean by "does not work for me"? I just tried this same code and it worked - created a new log file and put some text in it.
##logger is a class variable, it's a language issue, not Rails' one. I believe there's no need in further explanations :)
You've probably mistaken typing "function helloworld2" :)
Advanced Rails Recipes Recipe 84 from #topfunky shows how to define a custom logger. He has some code in the environment config file (production would look like this): RAILS_ROOT/config/environments/production.rb:
config.logger = RAILS_DEFAULT_LOGGER = Logger.new(config.log_path)
I'd test that out instead of redefining the class variable as you have. He might have something on http://nubyonrails.com to check as well.

Resources