I have written RUBY code for creating multipart form data request. It is working perfectly in ruby 1.9.3, but it is raising error in ruby 2.0 patch 247 and patch 0.
The error is,
/home/mohanrajr/Ruby/multipart/multipart_stream.rb:16:in `read': wrong number of arguments (2 for 1) (ArgumentError)
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:205:in `copy_stream'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:205:in `send_request_with_body_stream'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http/generic_request.rb:132:in `exec'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1404:in `block in transport_request'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from /home/mohanrajr/Ruby/multipart/multipart.rb:38:in `block in post'
from /home/mohanrajr/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/http.rb:852:in `start'
from /home/mohanrajr/Ruby/multipart/multipart.rb:38:in `post'
from multipart_post.rb:4:in `<main>'
Thanks for advance.
Related
I’m using Rails 6.1.4.4, Octoshark 0.3.0, and just upgraded to Ruby 3.0.2. Now when starting Rails console, I get
/Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:1043:in `establish_connection': wrong number of arguments (given 2, expected 1) (ArgumentError)
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/octoshark-0.3.0/lib/octoshark/active_record_extensions.rb:5:in `establish_connection'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/connection_handling.rb:52:in `establish_connection'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/railtie.rb:222:in `block (2 levels) in <class:Railtie>'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:42:in `each'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/lazy_load_hooks.rb:42:in `on_load'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activerecord-6.1.4.4/lib/active_record/railtie.rb:217:in `block in <class:Railtie>'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `instance_exec'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:32:in `run'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:61:in `block in run_initializers'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:228:in `block in tsort_each'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `call'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:226:in `tsort_each'
from /Users/myuser/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:205:in `tsort_each'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/initializable.rb:60:in `run_initializers'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/application.rb:391:in `initialize!'
from /Users/myuser/Documents/workspace/cfs/cfs-web/config/environment.rb:5:in `<top (required)>'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `require'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `block in require'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:299:in `load_dependency'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/activesupport-6.1.4.4/lib/active_support/dependencies.rb:332:in `require'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/application.rb:367:in `require_environment!'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/actions.rb:28:in `require_environment!'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/actions.rb:15:in `require_application_and_environment!'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/commands/console/console_command.rb:101:in `perform'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command/base.rb:69:in `perform'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/command.rb:48:in `invoke'
from /Users/myuser/.rvm/gems/ruby-3.0.2/gems/railties-6.1.4.4/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
I have this configured in config/initializers/octoshark.rb
CONN_MANAGER = Octoshark::ConnectionPoolsManager.new({
readonly: Rails.application.config.database_configuration['readonly'],
third_party_readonly: {
adapter: 'sqlserver',
username: APP_CONFIG.third_party_read_secondary[Rails.env].username,
password: APP_CONFIG.third_party_read_secondary[Rails.env].password,
host: APP_CONFIG.third_party_read_secondary[Rails.env].host,
database: APP_CONFIG.third_party_read_secondary[Rails.env].database
}
})
class ActiveRecord::Base
def self.connection
CONN_MANAGER.current_or_default_connection
end
end
Not sure what other changes to the above or other upgrades I would need to make to get my connection pooling configured properly and things loaded correctly.
Edit: Per the suggestion given, I created a file, lib/core_extensions/octoshark/connection_handler.rb, with this
module CoreExtensions
module Octoshark
module ConnectionHandler
def establish_connection(config, **parameters)
Octoshark::ConnectionPoolsManager.reset_connection_managers!
super(config, **parameters)
end
end
end
end
and then a file, config/initializers/ext.rb, with this
Dir.glob(Rails.root.join('lib/ext/**/*.rb')).sort.each do |filename|
require filename
end
but am still getting the error.
That has to do with how keyword arguments are handled in ruby 3.0 and how octoshark is passing its arguments to .establish_connection.
So, I think you could monkey-patch the gem method or work with a fork, where the parameters are explicitely passed to the active-record method:
module Octoshark
module ConnectionHandler
def establish_connection(config, **parameters)
Octoshark::ConnectionPoolsManager.reset_connection_managers!
super(config, **parameters)
end
end
end
I am experiencing two different errors - depending on whether I use Httparty in my Ruby app or I use it from the command line.
URL I am trying to access: http://apiv2.ahrefs.com/?token=$INSERT_VALID_AHREFS_API_TOKEN&from=backlinks&output=json&mode=domain&target=blog.ahrefs.com&limit=1000
HTTP method: GET
When I do so directly in bundle console I get Zlib::BufError:
HTTParty.get "http://apiv2.ahrefs.com/?token=$INSERT_VALID_AHREFS_API_TOKEN&from=backlinks_new_lost&output=json&mode=domain&target=blog.ahrefs.com&limit=1000"
Zlib::BufError: buffer error
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:357:in `finish'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:357:in `finish'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:262:in `ensure in inflater'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:262:in `inflater'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:274:in `read_body_0'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:201:in `read_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:226:in `body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:163:in `reading_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1414:in `block in transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `catch'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1378:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1371:in `block in request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:853:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1369:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty/request.rb:98:in `perform'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:539:in `perform_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:475:in `get'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:577:in `get'
from (irb):3
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/cli/console.rb:38:in `run'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/cli.rb:287:in `console'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor/command.rb:27:in `run'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor/invocation.rb:121:in `invoke_command'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor.rb:363:in `dispatch'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/vendor/thor/base.rb:440:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/cli.rb:9:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/bin/bundle:20:in `block in <top (required)>'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/lib/bundler/friendly_errors.rb:5:in `with_friendly_errors'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/bundler-1.6.2/bin/bundle:20:in `<top (required)>'
from /Users/medic/.rbenv/versions/2.1.2/bin/bundle:23:in `load'
from /Users/medic/.rbenv/versions/2.1.2/bin/bundle:23:in `<main>'irb(main):004:0>
Same error occurs for the following command:
Net::HTTP.get(URI('http://apiv2.ahrefs.com/?token=$AHREFS_API_TOKEN&from=backlinks_new_lost&output=json&mode=domain&target=blog.ahrefs.com&limit=1000'))
When I do it from the command line:
httparty "http://apiv2.ahrefs.com/?token=$INSERT_VALID_AHREFS_API_TOKEN&from=backlinks_new_lost&output=json&mode=domain&target=blog.ahrefs.com&limit=1000"
/Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/protocol.rb:153:in `read_nonblock': end of file reached (EOFError)
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/protocol.rb:153:in `rbuf_fill'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/protocol.rb:104:in `read'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:316:in `ensure in read_chunked'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:316:in `read_chunked'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:276:in `block in read_body_0'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:269:in `inflater'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:274:in `read_body_0'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:201:in `read_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:226:in `body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http/response.rb:163:in `reading_body'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1414:in `block in transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `catch'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1405:in `transport_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1378:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1371:in `block in request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:853:in `start'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/2.1.0/net/http.rb:1369:in `request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty/request.rb:98:in `perform'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:539:in `perform_request'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:475:in `get'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/lib/httparty.rb:577:in `get'
from /Users/medic/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/httparty-0.13.3/bin/httparty:90:in `<top (required)>'
from /Users/medic/.rbenv/versions/2.1.2/bin/httparty:23:in `load'
from /Users/medic/.rbenv/versions/2.1.2/bin/httparty:23:in `<main>'
The problem disappears when I reduce the limit query parameter to limit=10.
My assumption is that this is because the server returns a chunked response, if there is enough data.
I tested the URL with curl and it worked without a hitch. That's why I am assuming it is a problem in HTTParty
So I'm trying to do a migration that takes an existing column, which is a serialized array(text), and convert it into a psql array. I've tried a great number of things but keep getting an error.
This is what my migration looks like:
class RenameColumn < ActiveRecord::Migration
def up
rename_column :table_things, :column, :old_column
add_column :table_things, :column, :text, :array => true, null: false, :default => []
TableThing.class_eval do
serialize :old_column, Array
end
TableThing.reset_column_information
TableThing.find_each{|tt|
tt.column = tt.old_column
tt.save!
}
TableThing.reset_column_information
remove_column :table_things, :old_column
end
and this is the stack trace:
rake aborted!
An error has occurred, this and all later migrations canceled: ActiveRecord::RecordNotSaved/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/persistence.rb:125:in `save!'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/validations.rb:57:in `save!'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:275:in `block in save!'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:330:in `block in with_transaction_returning_status'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:275:in `save!'
/Users/username/code/work/Jobber/db/migrate/20140317215526_rename_addons.rb:15:in `block in up'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `block (2 levels) in find_each'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `each'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:26:in `block in find_each'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:76:in `find_in_batches'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:70:in `find_in_batches'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/relation/batches.rb:25:in `find_each'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/querying.rb:8:in `find_each'
/Users/username/code/work/Jobber/db/migrate/20140317215526_rename_addons.rb:13:in `up'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:578:in `exec_migration'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:559:in `block (2 levels) in migrate'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:558:in `block in migrate'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:557:in `migrate'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:713:in `migrate'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:963:in `block in execute_migration_in_transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:1009:in `block in ddl_transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:221:in `within_new_transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:1009:in `ddl_transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:962:in `execute_migration_in_transaction'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:924:in `block in migrate'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:920:in `each'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:920:in `migrate'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:768:in `up'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/migration.rb:746:in `migrate'
/Users/username/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.4/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/username/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
/Users/username/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'
Anyone have any ideas of what I'm doing wrong, or things I can try to fix my problem?
Changing from .save! to .save helped show better error messages, in the end the issue was with callbacks so changing from
TableThing.find_each{|tt|
tt.column = tt.old_column
tt.save!
}
to
TableThing.find_each{|tt|
tt.update_column(:column, tt.old_column)
}
Resolved the issue.
I have two very simple classes:
class Contact
include Mongoid::Document
has_and_belongs_to_many :organizations, :class_name => 'Organization'
end
and
class Organization
include Mongoid::Document
has_and_belongs_to_many :relations, :class_name => 'Contact'
end
and here is the error log upon validating / saving:
>> org = Organization.new
#<Organization _id: 5228ae3c1d41c8678c000001, relation_ids: nil, _type: "Organization">
>> org.valid?
org.valid?
NoMethodError: undefined method `has_key?' for #<Mongoid::Criteria:0x00000008d32bd0>
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/criteria.rb:501:in `method_missing'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/relations/referenced/many.rb:413:in `block in method_missing'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/scopable.rb:238:in `with_scope'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/relations/referenced/many.rb:412:in `method_missing'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/validatable.rb:72:in `read_attribute_for_validation'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:151:in `block in validate'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `each'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validator.rb:150:in `validate'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:283:in `_callback_before_145'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:377:in `_run__4324115989737504907__validate__callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/interceptable.rb:132:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:373:in `run_validations!'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `block in run_validations!'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__4324115989737504907__validation__callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/interceptable.rb:132:in `run_callbacks'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations/callbacks.rb:106:in `run_validations!'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/activemodel-4.0.0/lib/active_model/validations.rb:314:in `valid?'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/bundler/gems/mongoid-38de2e96edc8/lib/mongoid/validatable.rb:98:in `valid?'
from (irb):3
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /home/muichkine/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from /home/muichkine/src/white/bin/rails:4:in `require'
from /home/muichkine/src/white/bin/rails:4:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
>> contact = Contact.new
contact = Contact.new
#<Contact _id: 5228ae441d41c8678c000002, organization_ids: nil, _type: "Contact">
>> contact.valid?
true
as you can see, validating a Contact works while validating an Organization does not. Now, I did try to add :inverse_of options on both sides but the error remains. Any input is appreciated.
I use:
mongoid (4.0.0 38de2e9) with Rails 4 / Ruby 2.0.0-p247
So the problem is the name of the second has_and_belongs_to_many . If you change it to anything else it should work. Anyways thats a short solution for you, you should also file a issue on monogid, about that situation. I am pretty much sure this is happening because relations is a internal method on the Mongoid Document.
I'm in a Thor task where I'd like to add gems to the assets group in my gem file (see below).
The gem_group command (known from standard Rails App Templates) doesn't seem work in a Thor task (see error at bottom).
How could i best add a gem to a group in the gem file from within a Thor task?
Any thought welcome - thanks.
class ThorBuild < Thor
desc "add_gems", "Adds the gems I need"
def add_gems
puts "Running add_gems"
gem 'mongoid', '~> 3.0.15'
gem 'bson_ext'
gem 'less-rails'
gem 'devise'
gem_group :assets do
gem 'therubyracer', '= 0.10.0', :platforms => :ruby
gem 'twitter-bootstrap-rails'
end
end
end
The output looks like this ...
Running add_gems
/home/ubuntu/sites/ThorBuild.thor:12:in `add_gems': undefined method `gem_group' for #<Thor::Sandbox::ThorBuild:0x000000007b5110> (NoMethodError)
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/task.rb:27:in `run'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/invocation.rb:120:in `invoke_task'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor.rb:275:in `dispatch'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/base.rb:425:in `start'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/runner.rb:36:in `method_missing'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/task.rb:29:in `run'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/task.rb:126:in `run'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/invocation.rb:120:in `invoke_task'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor.rb:275:in `dispatch'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/lib/thor/base.rb:425:in `start'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/gems/thor-0.16.0/bin/thor:6:in `<top (required)>'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/thor:19:in `load'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/thor:19:in `<main>'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
from /home/ubuntu/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'