I am using Ruby on Rails 3.1.0 and DelayedJob. As many people on the web I get the "Job failed to load: uninitialized constant Syck::Syck
" error, but I think I discovered at least what generates the error (in my case). I have an ActiveModel like the following:
class Contact
include ActiveModel::Conversion
include ActiveModel::Validations
include ActiveModel::Dirty
extend ActiveModel::Naming
extend ActiveModel::Translation
attr_accessor :full_name, :email, :subject, :message
def initialize(attributes = {})
attributes.keys.each do |attr|
instance_variable_set "@" + attr.to_s, attributes[attr.to_sym]
end
end
validates_presence_of :full_name, :email, :subject, :message
def persist
@persisted = true
end
def persisted?
false
end
end
The related controller action is:
def contact
@contact = Contact.new(params[:contact])
if @contact.valid?
::Contact::Mailer.delay.contact(@contact)
respond_to do |format|
format.html { redirect_to root_path }
end
else
respond_to do |format|
format.html { render :action => :contact }
end
end
end
I noted that my problem with the "famous"\"infamous" Job failed to load: uninitialized constant Syck::Syck
happens only if I run the @contact.valid?
. If I re-implement the above controller action like this:
def contact
@contact = Contact.new(params[:contact])
::Contact::Mailer.delay.contact(@contact)
respond_to do |format|
format.html { redirect_to root_path }
end
end
all work as expected: I don't get the error and the e-mail is successfully sent. In few words, when I run @contact.valid?
inside the controller action (I can run that also without using the if ... else
statement) it generates the Job failed to load
error. I really do not understand this strange behavior related to the DelayedJob gem and the valid?
method.
Why it happens? How can I solve the problem?
More info at DelayedJob: “Job failed to load: uninitialized constant Syck::Syck”
UPDATES
If I debug the @contact.errors
in both cases using or not using the @contact.valid?
method...
... when I use the @contact.valid?
method (DelayedJob does not work) I get
#<ActiveModel::Errors:0x00000101759408 @base=#<Contact:0x000001017597f0 @full_name="Sample name", @email="foo@bar.com", @subject="Sample subject", @message="Sample message content.", @validation_context=nil, @errors=#<ActiveModel::Errors:0x00000101759408 ...>>, @messages={}>
... when I do not use the @contact.valid?
method (DelayedJob works) I get
#<ActiveModel::Errors:0x00000101759408 @base=#<Contact:0x000001017597f0 @full_name="Sample name", @email="foo@bar.com", @subject="Sample subject", @message="Sample message content.", @errors=#<ActiveModel::Errors:0x00000101759408 ...>>, @messages={}>
Note that in the second case the @validation_context=nil
is not present and that in both cases there is a "nested" <ActiveModel::Errors:0x0000010175940 ...>
statement. Is that a bug?