Using mailjet in rails - ruby-on-rails

I want to use Mailjet in order to send order confirmation email.
First of all, I installed Gem and configure as Mailjet guide
gem 'mailjet'
initializers/mailjet.rb
Mailjet.configure do |config|
config.api_key = '<my_api_key>'
config.secret_key = '<my_secret_key>'
config.default_from = '<my_email>'
end
config/application.rb
config.action_mailer.delivery_method = :mailjet_api
And this is action in my controller
def send_email_reserved
email = { :from_email => "<my_from_email>",
:from_name => "Dona Sky",
:subject => "Hello",
:text_part => "Hi",
:recipients => [{:email => "<my_to_email>"}] }
test = Mailjet::Send.create(email)
p test.attributes['Sent']
end
When I called the action, it showed the error
NoMethodError: undefined method `[]' for #<Set: {#<MIME::Type: application/json>}>
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:307:in `type_for_extension'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:312:in `type_for_extension'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:286:in `block (2 levels) in stringify_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:286:in `map'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:286:in `block in stringify_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `each'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `inject'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:272:in `stringify_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:92:in `make_headers'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:58:in `initialize'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `new'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/rest-client-1.6.7/lib/restclient/resource.rb:67:in `post'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/mailjet-1.3.8/lib/mailjet/connection.rb:67:in `handle_api_call'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/mailjet-1.3.8/lib/mailjet/connection.rb:47:in `post'
from /Users/Dona/.rvm/gems/ruby-2.2.2/gems/mailjet-1.3.8/lib/mailjet/resource.rb:215:in `save'

This is a dependency issue. Try adding this : gem rest-client '~>1.6.9' to your Gemfile and run bundle update

Related

Why does Capybara think I have an invalid URI scheme format? Addressable::URI::InvalidURIError: Invalid scheme format: 127.0.0.1

When I try to run this one specific test, it gives this error. When I try to run all tests, it fails for another reason. How can I run this one test without this error? It's clearly a valid URL.
C:\Users\Chloe\workspace\catalyst_research>rspec ./spec/features/ctdbase_purchase_spec.rb:28
http://ctdbase.org/ # output of puts
1) CTD Purchase Unregistered user visits the CTD product page directly and makes purchase
Failure/Error: visit url
Addressable::URI::InvalidURIError:
Invalid scheme format: 127.0.0.1
# ./spec/support/ctdbase_support.rb:8:in `visit_ctd'
# ./spec/features/ctdbase_purchase_spec.rb:11:in `block (2 levels) in <top (required)>'
Here is the code
ctdbase_support.rb
def visit_ctd(path)
url = "#{CTD_URL}#{path.starts_with?('/') ? '' : '/'}#{path}"
puts url
visit url # line 8
end
ctdbase_purchase_spec.rb
before do
delete_downloads
visit_ctd '/' # line 11
Here is the full backtrace:
C:\Users\Chloe\workspace\catalyst_research>rspec -b ./spec/features/ctdbase_purchase_spec.rb:28
...
1) CTD Purchase Unregistered user visits the CTD product page directly and makes purchase
Failure/Error: visit "#{CTD_URL}#{path.starts_with?('/') ? '' : '/'}#{path}"
Addressable::URI::InvalidURIError:
Invalid scheme format: 127.0.0.1
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:874:in `scheme='
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:799:in `block in initialize'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:2355:in `defer_validation'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:796:in `initialize'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:136:in `new'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:136:in `parse'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/template.rb:413:in `match'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `block in matches?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `any?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `matches?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:33:in `matches?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:58:in `block in request_stub_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `each'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `detect'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `request_stub_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:50:in `response_for_request'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:79:in `request'
# C:/ruby24/lib/ruby/2.4.0/net/http.rb:1165:in `get'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:82:in `block in responsive?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:123:in `start_without_connect'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:150:in `start'
# C:/ruby24/lib/ruby/2.4.0/net/http.rb:608:in `start'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:82:in `responsive?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:98:in `boot'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/session.rb:88:in `initialize'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara.rb:304:in `new'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara.rb:304:in `current_session'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/dsl.rb:45:in `page'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-rails-3.7.2/lib/rspec/rails/example/feature_example_group.rb:29:in `visit'
# ./spec/support/ctdbase_support.rb:6:in `visit_ctd'
# ./spec/features/ctdbase_purchase_spec.rb:11:in `block (2 levels) in <top (required)>'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:350:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:509:in `block in run_owned_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `each'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `run_owned_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:595:in `block in run_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:594:in `reverse_each'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:594:in `run_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:464:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:494:in `run_before_example'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:253:in `block in run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:500:in `block in with_around_and_singleton_context_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:466:in `block in run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:606:in `block in run_around_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:342:in `call'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-rails-3.7.2/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:375:in `execute_with'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:608:in `block (2 levels) in run_around_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:342:in `call'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:609:in `run_around_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:466:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:251:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:628:in `block in run_examples'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:624:in `map'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:624:in `run_examples'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:590:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `map'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1896:in `with_suite_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/reporter.rb:79:in `report'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:112:in `run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:87:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:71:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:45:in `invoke'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/exe/rspec:4:in `<top (required)>'
# C:/ruby24/bin/rspec:23:in `load'
# C:/ruby24/bin/rspec:23:in `<main>'
Seems this project is using WebMock. This is the webmock.rb file
spec/config/webmock.rb
require 'webmock/rspec'
WebMock.allow_net_connect!
GOOGLE_STUB_WITHOUT_IP = {
"city" => "Mountain View",
"region" => "California",
"region_code" => "CA",
"country" => "US",
"country_name" => "United States",
"continent_code" => "NA",
"postal" => "94035",
"latitude" => 37.386,
"longitude" => -122.0838,
"timezone" => "America/Los_Angeles",
"utc_offset" => "-0700",
"country_calling_code" => "+1",
"currency" => "USD",
"languages" => "en-US,es-US,haw,fr",
"asn" => "AS15169",
"org" => "Google LLC"
}
uri_template =
Addressable::Template.new "https://ipapi.co/{ip_address}/json/"
WebMock.stub_request(:get, uri_template).to_return { |request|
{
# FIXME: This should dynamically return the ip address
# body: GOOGLE_STUB_WITHOUT_IP.merge('ip' => request.ip_address)
body: GOOGLE_STUB_WITHOUT_IP.merge('ip' => '8.8.8.8').to_json,
headers: { 'Content-Type' => 'application/json' }
}
}
You're running into a bug in the combination of WebMock and Addressable - https://github.com/bblimke/webmock/issues/489 and https://github.com/bblimke/webmock/pull/739. The problem is that WebMock ends up creating variations of URLs to compare to templates, and ends up doing something like 127.0.0.1:<some_port>/__identify when Capybara starts the app. The proper solution is for WebMock to fix the URLs it's sending to Addressable, but you may be able to workaround it temporarily by setting Capybara.server_host='localhost' which will cause the URL variation to end up as localhost:<some_port>/__identify and Addressable will see localhost as a possible scheme (which could be valid). It's a hack, but it may hold you over until WebMock gets fixed. Another option would be to stop using URL templates with WebMock and use regex instead.

Getting Rspec error no implicit conversion of Symbol into Integer with Mongoid

I'm tying to test my Rails app with Rspec, but I'm getting a no implicit conversion of Symbol into Integer error without any apparent reason. Based on the traceback I get I think the problem is related to Mongo/Mongoid, however, I can't figure out what it is exactly. The code runs perfectly in production. The error happens only when testing.
Brief look at the model without the other methods:
class Card
include Mongoid::Document
field :front, type: String
field :back, type: String
field :level, type: Integer, default: 1
field :review_date, type: DateTime, default: DateTime.now
has_many :card_statistic, dependent: :destroy
belongs_to :topic
belongs_to :user
validates :front, :back, :level, presence: true
validates :topic, presence: { is: true, message: "must belong to a topic." }
validates :user, presence: { is: true, message: "must belong to a user." }
validates :level, numericality: { only_integer: true, greater_than: 0 }
end
One function in the model that triggers the error:
def self.reset(card)
card.update(level: 1)
end
The test code:
it "puts the given card in level 1" do
card = create(:card)
Card.correct card
card.reload
Card.correct card
card.reload
expect(card.level).to eq(3)
card.reset
card.reload
expect(card.level).to eq(1)
end
Then, the traceback of the error I get:
1) Card puts the given card in level 1
Failure/Error: Card.reset card
TypeError:
no implicit conversion of Symbol into Integer
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `[]'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `get'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/client.rb:170:in `read_preference'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:318:in `default_read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:251:in `read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/iterable.rb:38:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/query_cache.rb:207:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `block (2 levels) in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:562:in `with_sorting'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:229:in `block in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:474:in `try_cache'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:228:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual.rb:20:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/builders/referenced/in.rb:20:in `build'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:43:in `create_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:26:in `__build__'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:130:in `_loading'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:99:in `get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:187:in `block in getter'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:149:in `block in validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `public_send'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `block in make_lambda'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validate_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:395:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validation_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:334:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:97:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:371:in `invalid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:114:in `prepare_update'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:139:in `update_document'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/savable.rb:25:in `save'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:52:in `update'
# ./app/models/card.rb:57:in `reset'
# ./spec/models/card_spec.rb:32:in `block (2 levels) in <top (required)>'
The error is also triggered when testing the controllers. Even doing a get :index throws the error. Thanks in advance for your help.
Apparently, there where some issues with the very last builds of Mongoid and I was pulling the code directly from the repo without specifying a stable version. So the solution was to either downgrade the version to 4.0 or use the 5.0 beta build explicitly.
Either
gem 'mongoid', '~> 5.0.0.beta', github: 'mongoid/mongoid'
or
gem 'mongoid', '~> 4'
instead of
gem 'mongoid', '~> 5', github: 'mongoid/mongoid'

How to render rails mailer views in Mailgun HTTP API

I'm using Mailgun HTTP API to send emails from rails application.But I'm not able to render the mailer views. This is my mailer class.
class MailgunMails < ActionMailer::Base
def send_complex_message
data = Multimap.new
data[:from] = "Excited User <postmasters##{##domain}>"
data[:to] = "alice#example.com"
data[:subject] = "Hello"
data[:html] = File.open("#{Rails.root}/app/views/mailgun_mails/send_complex_message.html.erb", 'r').to_s.html_safe
## also tried
data[:html] = render(template: "send_complex_message.html.erb")
response = RestClient.post "https://api:#{##private_key}"\
"#api.mailgun.net/v2/#{##domain}/messages", data
response = JSON.parse(response)
end
end
But that throws:
NoMethodError: undefined method `each_byte' for nil:NilClass
How do I use mailer views with Mailgun API.
PS: I know there are many gems available to use mailgun with rails but none of them gives full functionality as the API does.
IF there is any alternative way to send emails from the api using the rails views, I'm waiting for that answer. I just don't know how to do that (even if it doesn't use actionmailer)
FULL error trace
NoMethodError: undefined method `each_byte' for nil:NilClass
from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:307:in `block in escape'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/core_ext/string/output_safety.rb:177:in `gsub'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/core_ext/string/output_safety.rb:177:in `gsub'
from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:304:in `escape'
from /Users/example/.rvm/rubies/ruby-2.0.0-p451/lib/ruby/2.0.0/uri/common.rb:623:in `escape'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:150:in `handle_key'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:143:in `block in build_stream'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:142:in `collect'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:142:in `build_stream'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:53:in `initialize'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:18:in `new'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/payload.rb:18:in `generate'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:45:in `initialize'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `new'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/rest-client-1.6.7/lib/restclient.rb:72:in `post'
... 2 levels...
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/callbacks.rb:18:in `block in process_action'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:373:in `_run__1281843520849433682__process_action__callbacks'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/callbacks.rb:17:in `process_action'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/base.rb:136:in `process'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/abstract_controller/rendering.rb:44:in `process'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:503:in `process'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:497:in `initialize'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:480:in `new'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/actionmailer-4.0.4/lib/action_mailer/base.rb:480:in `method_missing'
from (irb):1
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
from /Users/example/.rvm/gems/ruby-2.0.0-p451/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
How about using the render_to_string method? I believe you want to dynamically generate the view content. Reading up the view template by opening up the file would not run any Ruby code in it.
class MailgunMails < ActionMailer::Base
def send_complex_message
data = Multimap.new
data[:from] = "Excited User <postmasters##{##domain}>"
data[:to] = "alice#example.com"
data[:subject] = "Hello"
html_output = render_to_string template: "mailgun_mails/send_complex_message"
data[:html] = html_output.to_str
response = RestClient.post "https://api:#{##private_key}"\
"#api.mailgun.net/v2/#{##domain}/messages", data
response = JSON.parse(response)
end
end

sunspot:reindex error - getaddrinfo: nodename nor servname provided, or not known

I'm working with a Rails 3.2 application that has a mysql database and a number of models that are being indexed in Solr.
Here's what's happening:
I am running the following command:
RAILS_ENV=development bundle exec rake sunspot:reindex[1000] --trace
After indexing about 12% of the 4 million records (although it's a different percentage every time), the process inevitably bombs out with the following error and stack trace:
rake aborted!
getaddrinfo: nodename nor servname provided, or not known
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `initialize'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `open'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `block in connect'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/timeout.rb:54:in `timeout'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/timeout.rb:99:in `timeout'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:762:in `connect'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:755:in `do_start'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:744:in `start'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/net/http.rb:1284:in `request'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/connection.rb:15:in `execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/solr_instrumentation.rb:14:in `block in execute_with_as_instrumentation'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `block in instrument'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/notifications.rb:123:in `instrument'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/solr_instrumentation.rb:12:in `execute_with_as_instrumentation'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:167:in `execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:161:in `send_and_receive'
(eval):2:in `post'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:67:in `update'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/client.rb:87:in `add'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:106:in `add_documents'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/indexer.rb:30:in `add'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session.rb:91:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session_proxy/retry_5xx_session_proxy.rb:17:in `method_missing'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot.rb:184:in `index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:261:in `block (2 levels) in solr_index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:365:in `solr_benchmark'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:260:in `block in solr_index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/relation/batches.rb:72:in `find_in_batches'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/querying.rb:8:in `find_in_batches'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:259:in `solr_index'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/searchable.rb:203:in `solr_reindex'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/tasks.rb:64:in `block (3 levels) in <top (required)>'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/class_set.rb:16:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot-2.0.0/lib/sunspot/class_set.rb:16:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/sunspot_rails-2.0.0/lib/sunspot/rails/tasks.rb:63:in `block (2 levels) in <top (required)>'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:241:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:101:in `each'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/lib/rake/application.rb:70:in `run'
/Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rake-10.0.4/bin/rake:33:in `<top (required)>'
/Users/tchapin/.rbenv/versions/1.9.3-p392/bin/rake:23:in `load'
/Users/tchapin/.rbenv/versions/1.9.3-p392/bin/rake:23:in `<main>'
The app is running in development mode at localhost:3000, and solr is running at localhost:8982. Here's my solr.rake file:
Rake::Task['sunspot:reindex'].enhance ['sunspot:scope_models_for_index']
Rake::Task['sunspot:solr:reindex'].enhance ['sunspot:scope_models_for_index']
namespace 'sunspot' do
task :scope_models_for_index => :environment do
require 'rsolr/error'
Dir.glob(Rails.root.join('app/models/**/*.rb')).each { |path| require path }
# Add the GC
commit_extension = Module.new do
def commit
GC.start
super
end
end
Sunspot.extend commit_extension
# Set all the models default scopes the index scope
Sunspot.searchable.each do |model|
model.class_eval do
default_scope ->{ sunspot_index } if model.respond_to?(:sunspot_index)
end
end
end
end
Anyone know what might be causing this error, or how to fix it?
Apparently the problem is due to an exception: SocketError.
The exception occurs in the file /Users/tchapin/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rsolr-1.0.9/lib/rsolr/connection.rb:
def execute client, request_context
h = http request_context[:uri], request_context[:proxy], request_context[:read_timeout], request_context[:open_timeout]
request = setup_raw_request request_context
request.body = request_context[:data] if request_context[:method] == :post and request_context[:data]
begin
response = h.request request
charset = response.type_params["charset"]
{:status => response.code.to_i, :headers => response.to_hash, :body => force_charset(response.body, charset)}
rescue Errno::ECONNREFUSED => e
raise(Errno::ECONNREFUSED.new(request_context.inspect))
# catch the undefined closed? exception -- this is a confirmed ruby bug
rescue NoMethodError
$!.message == "undefined method `closed?' for nil:NilClass" ?
raise(Errno::ECONNREFUSED.new) :
raise($!)
end
end
It's not the prettiest fix, and it's obviously not a solution for production code, but I was able to temporarily solve the problem by adding a rescue handler for the SocketError exception, like so:
def execute client, request_context
h = http request_context[:uri], request_context[:proxy], request_context[:read_timeout], request_context[:open_timeout]
request = setup_raw_request request_context
request.body = request_context[:data] if request_context[:method] == :post and request_context[:data]
retries = 10
begin
response = h.request request
charset = response.type_params["charset"]
{:status => response.code.to_i, :headers => response.to_hash, :body => force_charset(response.body, charset)}
rescue Errno::ECONNREFUSED => e
raise(Errno::ECONNREFUSED.new(request_context.inspect))
# catch the undefined closed? exception -- this is a confirmed ruby bug
rescue NoMethodError
$!.message == "undefined method `closed?' for nil:NilClass" ?
raise(Errno::ECONNREFUSED.new) :
raise($!)
rescue SocketError => e
puts e
if retries > 0
puts "SocketError! Retrying connection after 1 second..."
retries -= 1
sleep(1)
retry
else
puts "SocketError: Not responding after 10 retries! Giving up!")
exit
end
end
end
Additionally, it looks like my local copy of Solr was having some issues with running out of memory. I solved the memory problem by updating my sunspot.yml file accordingly:
development:
solr:
hostname: 127.0.0.1
port: 8982
min_memory: 512M
max_memory: 2G
log_level: INFO
# open_timeout: 3
# read_timeout: 3

Old Ruby bug is recurring in my Ruby on Rails app, related to Class.create and delayed_job

The bug is many months old, here:
http://www.ruby-forum.com/topic/1094002
Two links in that which show code changes:
https://github.com/godfat/ruby/commit/f4e0e8f781b05c767ad2472a43a4ed0727a75708
https://github.com/godfat/ruby/commit/c7a6cf975d88828c2ed27d253f41c480f9b66ad6
I have Ruby 1.9.2 and rvm. I would have pasted those changes into the appropriate files, but I don't know how.
This worked a few days ago. I can't do Ruby on Rails commands like:
>> User.create :username => "a", :password => "a"
Here is the error message:
ArgumentError: wrong number of arguments(1 for 0)
from /Users/RedApple/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:48:in `method'
from /Users/RedApple/.rvm/rubies/ruby-1.9.2 p290/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:48:in `accept'
from /Users/RedApple/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/visitors/yaml_tree.rb:36:in `<<'
from /Users/RedApple/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:165:in `dump'
from /Users/RedApple/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych/core_ext.rb:13:in `psych_to_yaml'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/delayed_job-2.0.4/lib/delayed/backend/base.rb:57:in `payload_object='
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:2918:in `block in assign_attributes'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:2914:in `each'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:2914:in `assign_attributes'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:2787:in `attributes='
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:2477:in `initialize'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:725:in `new'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:725:in `create'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/delayed_job-2.0.4/lib/delayed/message_sending.rb:9:in `method_missing'
from /Users/RedApple/S/app/models/user.rb:29:in `block in <class:User>'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-2.3.14/lib/active_support/callbacks.rb:182:in `call'
... 7 levels...
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/callbacks.rb:267:in `create_with_callbacks'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:2927:in `create_or_update'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/callbacks.rb:250:in `create_or_update_with_callbacks'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:2577:in `save'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/validations.rb:1089:in `save_with_validation'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/dirty.rb:79:in `save_with_dirty'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/transactions.rb:229:in `block in with_transaction_returning_status'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/transactions.rb:182:in `transaction'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/transactions.rb:196:in `block in save_with_transactions'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/transactions.rb:196:in `save_with_transactions'
from /Users/RedApple/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-2.3.14/lib/active_record/base.rb:727:in `create'
from (irb):1
from /Users/RedApple/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'>
Line 28-30 in user.rb:
after_create do |user|
user.delay( :priority => -15 ).seed
end
I am dead in the water without this. Can anyone help?
Delayed Job and the Ruby 1.9.2 YAML parser (Psych) are unfortunately not compatible.
Switch back to the previous YAML parser (Syck) by adding this to your config/application.rb, just below the call to Bundler.require:
YAML::ENGINE.yamler = "syck" if RUBY_VERSION >= "1.9.2"
You could patch your Ruby, but it's not easy and not really possible on managed hosts.
My solution was to create a monkeypatch based on the patch commit 31075.
Create an initializer file in your Rails project
config/initializers/psych_extensions.rb
and add this code:
# APPLIES RUBY PATCH REVISION 31075
module Psych
module Visitors
###
# YAMLTree builds a YAML ast given a ruby object. For example:
#
# builder = Psych::Visitors::YAMLTree.new
# builder << { :foo => 'bar' }
# builder.tree # => #<Psych::Nodes::Stream .. }
#
class YAMLTree < Psych::Visitors::Visitor
def accept target
# return any aliases we find
if node = #st[target.object_id]
node.anchor = target.object_id.to_s
return #emitter.alias target.object_id.to_s
end
if target.respond_to?(:to_yaml)
loc = target.public_method(:to_yaml).source_location.first
if loc !~ /(syck\/rubytypes.rb|psych\/core_ext.rb)/
unless target.respond_to?(:encode_with)
if $VERBOSE
warn "implementing to_yaml is deprecated, please implement \"encode_with\""
end
target.to_yaml(:nodump => true)
end
end
end
if target.respond_to?(:encode_with)
dump_coder target
else
send(#dispatch_cache[target.class], target)
end
end
private
# FIXME: remove this method once "to_yaml_properties" is removed
def find_ivars target
loc = target.public_method(:to_yaml_properties).source_location.first
unless loc.start_with?(Psych::DEPRECATED) || loc.end_with?('rubytypes.rb')
if $VERBOSE
warn "#{loc}: to_yaml_properties is deprecated, please implement \"encode_with(coder)\""
end
return target.to_yaml_properties
end
target.instance_variables
end
end
end
end

Resources