Mongoid Habtm relationship with specified class_name - mongoid

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.

Related

How to configure Ocotshark (connection pooling) to work with Ruby 3 and Rails 6.1?

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

"End of file reached" error OR Zlib::BufError using HTTParty

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

Converting a serialized array to a PSQL array, error

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.

Ruby 2.0 Create Multipart Form Data

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.

Rails3 sunspot:reindex - rake aborted! undefined method [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a rails model that has multiple "has_many through" relationships.
I defined these in active record and in the searchable block.
When I define the relationship on a single-word model name
(ex: Contributor) and re-index, it works fine.
When I define the relationship on a 2-word model name
(ex: LuSubject) and re-index, it does not work. I get the error "undefined method 'subject_ids'.
I tried other models with the same result. "Award" works. "LuGrade" does not.
Here is the code from my "Work" model:
class Work < ActiveRecord::Base
self.table_name = 'work'
self.primary_keys = :WORK_ID
alias_attribute :work_id, :WORK_ID
alias_attribute :title, :TITLE
alias_attribute :series_id, :SERIES_ID
has_many :work_contributors, :foreign_key => :WORK_ID
has_many :contributors, :through => :work_contributors
has_many :work_grades, :foreign_key => :WORK_ID
has_many :lu_grades, :through => :work_grades
has_many :work_primary_subjects, :foreign_key => :WORK_ID
has_many :lu_subjects, :through => :work_primary_subjects
has_many :work_awards, :foreign_key => :WORK_ID
has_many :awards, :through => :work_awards
belongs_to :series, :foreign_key => :SERIES_ID
searchable do
text :title
integer :work_id
string :title
integer :series_id
integer :contributor_ids, :references => Contributor, :multiple => true
integer :subject_ids, :references => LuSubject, :multiple => true
end
Here is the output of the reindex command
$ bundle exec rake sunspot:reindex
rake aborted!
undefined method `subject_ids' for #<Work:0x00000003bee760>
Tasks: TOP => sunspot:reindex
(See full trace by running task with --trace)
Here is the full trace
$ bundle exec rake sunspot:reindex --trace
** Invoke sunspot:reindex (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute sunspot:reindex
rake aborted!
undefined method `subject_ids' for #<Work:0x00000004001690>
/var/lib/gems/1.9.1/gems/activemodel-3.2.6/lib/active_model/attribute_methods.rb:407:in `method_missing'
/var/lib/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/attribute_methods.rb:149:in `method_missing'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/data_extractor.rb:17:in `value_for'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/field_factory.rb:58:in `populate_document'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:95:in `block in prepare '
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:94:in `each'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:94:in `prepare'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:24:in `block in add'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:24:in `map'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/indexer.rb:24:in `add'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/session.rb:91:in `index'
/var/lib/gems/1.9.1/gems/sunspot- 1.3.3/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/var/lib/gems/1.9.1/gems/sunspot- 1.3.3/lib/sunspot/session_proxy/retry_5xx_session_proxy.rb:17:in `method_missing'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/session_proxy/abstract_session_proxy.rb:11:in `index'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot.rb:184:in `index'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:256:in `block (2 levels) in solr_index'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:345:in `solr_benchmark'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:255:in `block in solr_index'
/var/lib/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/relation/batches.rb:72:in `find_in_batches'
/var/lib/gems/1.9.1/gems/activerecord-3.2.6/lib/active_record/querying.rb:8:in `find_in_batches'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:254:in `solr_index'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/searchable.rb:198:in `solr_reindex'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/tasks.rb:58:in `block (3 levels) in <top (required)>'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/class_set.rb:16:in `each'
/var/lib/gems/1.9.1/gems/sunspot-1.3.3/lib/sunspot/class_set.rb:16:in `each'
/var/lib/gems/1.9.1/gems/sunspot_rails-1.3.3/lib/sunspot/rails/tasks.rb:57:in `block (2 levels) in <top (required)>'
/usr/lib/ruby/vendor_ruby/rake/task.rb:205:in `call'
/usr/lib/ruby/vendor_ruby/rake/task.rb:205:in `block in execute'
/usr/lib/ruby/vendor_ruby/rake/task.rb:200:in `each'
/usr/lib/ruby/vendor_ruby/rake/task.rb:200:in `execute'
/usr/lib/ruby/vendor_ruby/rake/task.rb:158:in `block in invoke_with_call_chain'
/usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/usr/lib/ruby/vendor_ruby/rake/task.rb:151:in `invoke_with_call_chain'
/usr/lib/ruby/vendor_ruby/rake/task.rb:144:in `invoke'
/usr/lib/ruby/vendor_ruby/rake/application.rb:116:in `invoke_task'
/usr/lib/ruby/vendor_ruby/rake/application.rb:94:in `block (2 levels) in top_level'
/usr/lib/ruby/vendor_ruby/rake/application.rb:94:in `each'
/usr/lib/ruby/vendor_ruby/rake/application.rb:94:in `block in top_level'
/usr/lib/ruby/vendor_ruby/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/vendor_ruby/rake/application.rb:88:in `top_level'
/usr/lib/ruby/vendor_ruby/rake/application.rb:66:in `block in run'
/usr/lib/ruby/vendor_ruby/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/vendor_ruby/rake/application.rb:63:in `run'
/usr/bin/rake:27:in `<main>'
Tasks: TOP => sunspot:reindex
Here is my current version info
$ gem list|grep sunspot
sunspot (1.3.3)
sunspot_rails (1.3.3)
sunspot_solr (1.3.3)
NOTE: I am working with production tables, so I don't have the option of renaming those tables.
Thanks.
Your setting of integer :subject_ids, ... will cause Sunspot to invoke a subject_ids method on your object. Some ActiveRecord associations will define a method like that based on a has_many association, but I don't see a has_many :subjects which would define that.
Did you mean integer :lu_subject_ids?

Resources