Rails : gem "dynamoid" list_tables error - ruby-on-rails

I'm not new to DynamoDB, but I'm new to DynamoID gem.
I can't achieve to make it work since I get the following error :
/aws-sdk-1.28.1/lib/aws/core/client.rb:366:in `return_or_raise': The action or operation requested is invalid. Verify that the action is typed correctly. (AWS::DynamoDB::Errors::InvalidAction)
/aws-sdk-1.28.1/lib/aws/core/client.rb:467:in `client_request'
(eval):3:in `list_tables'
/aws-sdk-1.28.1/lib/aws/dynamo_db/table_collection.rb:121:in `_each_item'
/aws-sdk-1.28.1/lib/aws/core/collection/with_limit_and_next_token.rb:54:in `_each_batch'
/aws-sdk-1.28.1/lib/aws/core/collection.rb:80:in `each_batch'
/aws-sdk-1.28.1/lib/aws/core/collection.rb:47:in `each'
/dynamoid-0.7.1/lib/dynamoid/adapter/aws_sdk.rb:185:in `collect'
/dynamoid-0.7.1/lib/dynamoid/adapter/aws_sdk.rb:185:in `list_tables'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:146:in `block (3 levels) in <module:Adapter>'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:39:in `benchmark'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:146:in `block (2 levels) in <module:Adapter>'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:25:in `block in reconnect!'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:39:in `benchmark'
/dynamoid-0.7.1/lib/dynamoid/adapter.rb:25:in `reconnect!'
/dynamoid-0.7.1/lib/dynamoid.rb:35:in `configure'
app/config/initializers/dynamoid.rb:1:in `<top (required)>'
It seems that list_tables doesn't exist, but it's a method of the AWS Ruby SDK
My model is very simple :
class Interaction
include Dynamoid::Document
table :name => :interactions, :key => :from, :read_capacity => 1, :write_capacity => 1
end
Could anybody help?
Cheers,
Emmanuel

Related

Sidekiq Job Fails in Production | NameError: uninitialized constant

I have an Expenditure model:
class Expenditure < ApplicationRecord
multi_tenant :company
after_commit :related_reindex
def related_reindex
ExpenditureRelatedReindex.perform_async(id)
end
end
Here is my worker expenditure_related_reindex.rb:
class ExpenditureRelatedReindex
include Sidekiq::Worker
sidekiq_options :queue => :critical, :retry => true, :backtrace => true
def perform(record_id)
e = Expenditure.find(record_id)
MultiTenant.with(e.company) do
return unless e
e.expenditure_items.each(&:reindex)
e.children&.each(&:reindex)
end
end
end
The reindex can take some time so I want these to spin off to SideKiq. I have sime multi tenant code I should mention but I don't think it's the issue. After the record is updated I get:
NameError: uninitialized constant ExpenditureRelatedReindex
Did you mean? ExpenditureItemsHelper
from sidekiq/processor.rb:268:in `const_get'
from sidekiq/processor.rb:268:in `constantize'
from sidekiq/processor.rb:132:in `block (5 levels) in dispatch'
from sidekiq/rails.rb:43:in `block in call'
from active_support/execution_wrapper.rb:87:in `wrap'
from active_support/reloader.rb:73:in `block in wrap'
from active_support/execution_wrapper.rb:87:in `wrap'
from active_support/reloader.rb:72:in `wrap'
from sidekiq/rails.rb:42:in `call'
from sidekiq/processor.rb:131:in `block (4 levels) in dispatch'
from sidekiq/processor.rb:257:in `stats'
from sidekiq/processor.rb:126:in `block (3 levels) in dispatch'
from sidekiq/job_logger.rb:13:in `call'
from sidekiq/processor.rb:125:in `block (2 levels) in dispatch'
from sidekiq/job_retry.rb:78:in `global'
from sidekiq/processor.rb:124:in `block in dispatch'
from sidekiq/logger.rb:10:in `with'
from sidekiq/job_logger.rb:33:in `prepare'
from sidekiq/processor.rb:123:in `dispatch'
from sidekiq/processor.rb:162:in `process'
from sidekiq/processor.rb:78:in `process_one'
from sidekiq/processor.rb:68:in `run'
from sidekiq/util.rb:15:in `watchdog'
from sidekiq/util.rb:24:in `block in safe_thread'
I have even taken out the entire 'perform' block of code i.e. the worker does nothing to confirm I don't have some sort of regressive call etc. I confirm that my other workers fire and process just fine. Checked for obvious typos - banging my head against the wall at this point.
UPDATE
Ok - I have confirmed one thing - if I add any new workers with any name this triggers the same error. I even rebooted the entire production server to confirm the whole code was reloaded etc.
It ended up being Redis (or at least my fix was related). Found this post:
https://github.com/mperham/sidekiq/issues/2834#issuecomment-184800981
and it was a Redis namespace conflict. My server does have elasticsearch running also so that makes sense. I am not sure why the old workers run but the new ones failed. My fix looks like this:
config.redis = {
url: ENV['REDIS_URL'],
namespace: "some_namespace_different_for_each_app"
}
You also need the redis-namespace gem BTW

PgSearch 1.0.0 throws ModelNotMultisearchable error when trying to rebuild from console

I've got an app with 4 different models which are multisearchable, however they were originally set up with a pg_search_scope; ie:
class Episode < ActiveRecord::Base
include PgSearch
# multisearchable :against => [:title, :description]
pg_search_scope :search_text,
:against => [:title, :description],
:using => {
:tsearch => {:prefix => true}
}
As you can see at one point I tried setting this up the multiserachable way.
Of late there are items we know should be returning that are NOT in the search results, and I thought I'd have to rebuild the pg_search_documents; however that's throwing an error:
irb(main):004:0> PgSearch::Multisearch.rebuild(Show)
PgSearch::Multisearch::ModelNotMultisearchable: PgSearch::Multisearch::ModelNotMultisearchable
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch/rebuilder.rb:6:in `initialize'
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch.rb:10:in `new'
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch.rb:10:in `block in rebuild'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch.rb:8:in `rebuild'
from (irb):4
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from /app/bin/rails:4:in `require'
from /app/bin/rails:4:in `<main>'
irb(main):005:0>
I thought that it might be because of the setup as a pg_search_scope, so I set one class as above with the multisearch:multisearchable :against => [:title, :description] -- but even then I get rebuild problems.
I tried the manual way of deleting from the pg_search table and rebuilding but even THAT's throwing an error:
2.1.0 :010 > Show.find_each{ |record| record.update_pg_search_document }
Show Load (4.8ms) SELECT "shows".* FROM "shows" ORDER BY "shows"."id" ASC LIMIT 1000
NoMethodError: undefined method `update_pg_search_document' for #<Show:0x007fd945a92358>
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods.rb:155:in `method_missing'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/acts_as_follower-0.2.1/lib/acts_as_follower/followable.rb:54:in `method_missing'
from (irb):10:in `block in irb_binding'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:26:in `block (2 levels) in find_each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:26:in `each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:26:in `block in find_each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:75:in `find_in_batches'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:70:in `find_in_batches'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:25:in `find_each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/querying.rb:8:in `find_each'
from (irb):10
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4
I've been getting the same error while trying to rebuild (for the first time, installing) via the Rails command line with PgSearch::Multisearch.rebuild(Professor). What's worked for me is instead rebuilding from the command line using the alternative approach recommended on the Github:
rake pg_search:multisearch:rebuild[Professor]
This seems to work for me -- it doesn't throw an error, and when I go to the Rails console after and run PgSearch.multisearch("query") it's returning a result, whereas before it was returning an error.
I know this isn't a fix, but it's a solution to the immediate problem. I hope it helps -- posting an issue on their Github might lead to a fuller fix.

find method ArgumentError "wrong number of arguments (1 for 0)"

In rails console..
Physician.find(1285849521) results in
ArgumentError: wrong number of arguments (1 for 0)
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `initialize'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `new'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `substitute_at'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:989:in `block in create_binds'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:987:in `each'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:987:in `each_with_index'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:987:in `create_binds'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:962:in `build_where'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:586:in `where!'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/relation/query_methods.rb:576:in `where'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/querying.rb:10:in `where'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/core.rb:150:in `block (2 levels) in find'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/statement_cache.rb:80:in `call'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/statement_cache.rb:80:in `create'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activerecord-4.2.0.beta4/lib/active_record/core.rb:149:in `block in find'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/2.1.0/mutex_m.rb:73:in `synchronize'
... 13 levels...
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `load'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `block in load'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:237:in `load_dependency'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/activesupport-4.2.0.beta4/lib/active_support/dependencies.rb:246:in `load'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/tim/.rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
and of course..
Physician.find() results in "ActiveRecord::RecordNotFound: Couldn't find Physician without an ID"
My physician.rb is
class Physician < ActiveRecord::Base
self.table_name = "t_phys"
self.primary_key = "sln"
end
I'm using a legacy postgres db, the table t_phys has a natural key called sln, which is a 10 digit integer. There is a physician in the db with sln 1285849521 (the integer in the find method above), which I know because Physician.all() works properly and that's the first record that is displayed.
What am I doing wrong? How can I get the find method to work properly? I'm just getting started with rails again, probably a newbie mistake somewhere. Appreciate the help!
For more detail, when I got to localhost:3000/physicians/1285849521, the page renders:
ArgumentError in PhysiciansController#show
Wrong number of arguments (1 for 0)
Extracted source (around line #160):
159 def substitute_at(column, index)
160 Arel::Nodes::BindParam.new "$#{index + 1}"
161 end
162
163 def exec_query(sql, name = 'SQL', binds = [])
I can provide the full stack trace if necessary.
Seems like it should work, but I'm wondering if the custom primary key is still messing with find.
Maybe this background will help confirm one way or the other:
http://ruby-journal.com/how-to-override-default-primary-key-id-in-rails/
Thanks to Todd Agulnick's comment above, I updated to Rails 4.2 rc2 and the error went away. I encountered a new error, which I resolved and is outside the scope of this question. Thanks Todd!

Has and belongs to many association search in thinking sphinix rails 3.2.14

I am using Rails 3.2.14, thinking-sphinx 3.1.1 and Sphinx 2.2.4-id64-release (r4806)
I have two modals below:-
Album.rb
has_and_belongs_to_many :genres
Genre.rb
has_and_belongs_to_many :albums
So the associated table is albums_genres
I want to search albums using the genre_id
Example: When I pass a genre_id then it should return all the albums associated to that genre_id on the basis of third table albums_genres
I have tried like that
ThinkingSphinx::Index.define :album, :with => :active_record do
#other indexes
has genres.id, :as => :genre_ids, :source => :query
end
But when i run the rake task as rake ts:index i am getting the error as:-
rake aborted!
undefined method `join_table' for #<ActiveRecord::Reflection::AssociationReflection:0x000000079224d8>
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:15:in `quoted_foreign_key'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:28:in `to_sql'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:76:in `queries'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/simple_many_query.rb:5:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/property_query.rb:15:in `to_s'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:45:in `query'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:38:in `multi_declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:23:in `declaration'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:96:in `block in append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `each'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:93:in `append_presenter_to_attribute_array'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:132:in `prepare_for_render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/sql_source.rb:65:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration/index.rb:29:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/index.rb:53:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `block in render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `collect'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/riddle-1.5.11/lib/riddle/configuration.rb:43:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:90:in `render'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `block in render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `render_to_file'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:13:in `configure'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:24:in `index'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `eval'
/home/shamsul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_executable_hooks:15:in `<main>'
It looks like this is a bug with Rails 3.2, :source => :query and HABTM joins (which is sadly not surprising - getting the code to work even with Rails 4.0 was painful and fragile).
I'm afraid your options are either upgrade to Rails 4 or not use the :source => :query option (and I know that means you lose some nice speed gains for indexing).
Odds of me getting a patch sorted in the near future is small (but others are welcome to give it a shot, if they wish).

Rails Mongoid / MongoHD / Heroku error "need to login" when saving to database

I have an error that appears to be connected to these two links:
https://groups.google.com/forum/#!msg/mongoid/L6ESKP8QDpg/gmEyqejdAEEJ
https://github.com/mongoid/moped/pull/59
See console log below. I am running with Mongoid/MongoHQ, when I create an object (apparently any database object) I get this "need to login" error. Note however that the database object IS created and is in the database after the error.
Is there some way I can fix this myself, or perhaps it's something that MongoHQ needs to fix?
$ heroku run console
irb(main):004:0> Request.create!(:from_user => User.first, :to_user => User.first)
Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
#length=69
#request_id=7
#response_to=0
#op_code=2004
#flags=[]
#full_collection_name="admin.$cmd"
#skip=0
#limit=-1
#selector={:getlasterror=>1, :safe=>true}
#fields=nil>
failed with error "need to login"
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:74:in `block in command'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:521:in `[]'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:521:in `block (3 levels) in flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:520:in `map'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:520:in `block (2 levels) in flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:123:in `ensure_connected'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:516:in `block in flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:531:in `logging'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:515:in `flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:286:in `pipeline'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/session/context.rb:58:in `block in insert'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/session/context.rb:109:in `block in with_node'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/cluster.rb:150:in `block in with_primary'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:168:in `ensure_primary'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/cluster.rb:149:in `with_primary'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/session/context.rb:108:in `with_node'
... 13 levels...
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/callbacks.rb:100:in `run_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence/insertion.rb:23:in `prepare'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence.rb:50:in `insert'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence.rb:251:in `block in create!'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/threaded/lifecycle.rb:173:in `_creating'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence.rb:249:in `create!'
from (irb):4
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'irb(main):005:0>
I am having the exact same problem. This is not exactly a solution but you can work around the problem by setting the 'safe' option in mongoid.yml to false. It would look similar to this:
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: false
It turns out the problem is that MongoHQ upgraded their MongoDB and it introduced an incompatibility with mongoid's stable branch.
Chris Winslett fixed the bug and durran pulled it into mongoid master. But it was not included in the stable branch when I posted this stackoverflow.
Solutions:
I solved the problem by updating my gemfile to pull mongoid from github.
durran has now included it in stable-1.2.0 so you should get the fix if you gem update.

Resources