I need to use the two gems mongoid and mongo in my Rails app.
Any ideas to workaround this troublesome problem thanks~
gem 'mongo', '2.0.2',:git => 'git://github.com/mongodb/mongo-ruby-driver.git'
gem 'mongoid', '4.0.0'
# gem "mongo_mapper"
Error message
Bundler could not find compatible versions for gem "bson":
In Gemfile:
mongo (= 2.0.2) ruby depends on
bson (~> 3.0) ruby
mongoid (= 4.0.0) ruby depends on
moped (~> 2.0.0) ruby depends on
bson (~> 2.2) ruby
UPDATE
Mongoid is handy , but sometime it is sucks,
because I got error 'Client Error: Remaining data too small for BSON object,
and even I wrapped it in begin rescue block , it still break my Rails app, and stop the import data task, what the suck Mongoid is ! So I decided to give up use Mongoid to insert new records to collection , I tried to use mongo. But they are conflict in my Rails app, WTH again !
def bulk_insert_to_collection(collection_name)
model = collection_name.capitalize.constantize.new
begin
model.collection.insert(#data_in_chunk)
# #bulk.execute
rescue Exception=>err
log_error "[#{err}] File #{#file_path}: line of #{i} #{Time.now}"
rescue Exception => err
log_error "[#{err}] File #{#file_path}: line of #{i} #{Time.now}"
rescue => err
p ex
log_error "[#{err}] File #{#file_path}: line of #{i} #{Time.now}"
end
# #data_in_chunk = []
end
Related
I have a Rails 4.2 framework and use Kaminari to paginate. Everything works great, but I want to test everything with Capybara, thus I run into the page conflict. So I wanted to rename the page_method_name of Kaminari and followed the guide from them:
$ bundle exec rails g Kaminari:config
This results in this file
app/config/initializers/kaminari_config.rb
Kaminari.configure do |config|
config.page_method_name = :plant
end
In which I simply uncommented the config.page_method_name and set it to :plant (as an example from the Kaminari docs to avoid any reserved method name conflicts for something like :kaminari_page).
Then I adjusted the appropriate controller to
def index
#q = Interaction.published.order(updated_at: :desc).limit(200).ransack(params[:q])
#selection = #q.result
#interactions = Kaminari.paginate_array(#selection).plant(params[:page]).per(10)
respond_to do |format|
format.html
format.js
end
end
I restarted everything and got this error when visiting the
undefined method `plant' for #Kaminari::PaginatableArray:0x00005568fb42ba30
Gemfile.lock:
kaminari (1.2.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.1)
kaminari-activerecord (= 1.2.1)
kaminari-core (= 1.2.1)
kaminari-actionview (1.2.1)
actionview
kaminari-core (= 1.2.1)
kaminari-activerecord (1.2.1)
activerecord
kaminari-core (= 1.2.1)
kaminari-core (1.2.1)
I was then googling around and also added require 'kaminari' (which does not make so much sense, because I did not get the uninit constant error) and also included the Kaminari configure section into the class Application < Rails::Application inside config/application.rb.
Nothing worked, always the same error.
You show your files location as app/config/initializiers/kaminari_config.rb. If that is the actual location of your file it fully explains why the method doesn't exist. The location should be app/config/initializers/kaminari_config.rb. With the location you specified it wouldn't be automatically loaded when the app starts.
If the location you showed is just a type then that wouldn't be the cause, but if you didn't include Capybara into the global object, which you shouldn't be doing anyway, then you wouldn't have a name collision. Current versions of Capybara issue a warning when you do include it into the global object specifically for this reason.
Also is there any reason you're grabbing all the results and then paging them, rather than using the page (or plant) and per scopes that would be defined on Interaction?
Resource form contents:
form do |f|
f.inputs do
f.input :name
end
f.actions
end
I am using Rails 5 beta 3, here is Gemfile contents:
# Backend
gem 'activeadmin', github: 'activeadmin/activeadmin', branch: 'master'
Gemfile.lock contents:
GIT
remote: git://github.com/activeadmin/activeadmin.git
revision: f7483e3b8fcd74437b03c18fb658dac62a9fc62e
branch: master
specs:
activeadmin (1.0.0.pre2)
arbre (~> 1.0, >= 1.0.2)
bourbon
coffee-rails
formtastic (~> 3.1)
formtastic_i18n
inherited_resources (~> 1.6)
jquery-rails
jquery-ui-rails
kaminari (~> 0.15)
rails (>= 3.2, < 5.0)
ransack (~> 1.3)
sass-rails
sprockets (< 4)
Even attribute is included in permitted params:
permit_params :name
it's missing in params when I submit empty value and as a result name is not updated. Non-empty values work OK.
Same with select boxes.
After error occured, I tried to update Active Admin with:
bundle update activeadmin
but error still remains.
I tested it on simple rails form (generated by scaffold command) and formtastic outside of Active Admin resource, both options work, so it seems to be Active Admin problem.
Here is how I checked params content (also checked logs/development.log):
controller do
def update
abort params.inspect
end
end
So name is not presented even at this moment.
I posted issue here but no feedback till now.
Since you are using rails5.0.0beta2, you are probably also using rack 2.0.0.alpha.
This is caused by a bug in rack.
Until rack2.0.0 become stable, you can solve this bug by adding to the Gemfile:
gem 'rack', github: 'rack/rack'
So I've decided to split my last post since the bug is Related to ByeBug more than to Pry-Remote (I think). Last post URL: Pry-Remote not triggered Rails 4
Problem:
When typing Next in Pry-Remote, ByeBug acts unexpectedly and goes to "teardown".
Also created an ByeBug Issue:
https://github.com/deivid-rodriguez/pry-byebug/issues/78
What I am running:
gem 'pry-byebug', '=1.3.3'
gem 'pry-stack_explorer'
gem 'pry-rails'
gem 'pry-remote'
pry (0.10.2)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (1.3.3)
byebug (~> 2.7)
pry (~> 0.10)
pry-rails (0.3.4)
pry (>= 0.9.10)
pry-remote (0.1.8)
pry (~> 0.9)
slop (~> 3.0)
pry-stack_explorer (0.4.9.2)
binding_of_caller (>= 0.7)
pry (>= 0.9.11)
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
Rails 4.2.4
Note 1:
Updated all the pry related gems
https://gist.github.com/YOUConsulting/65cdcdc22d32780dde51
You seem to be using an ancient version of pry-byebug, the latest as of this writing is 3.2.0 but you're using 1.3.3. If you update your gems there's a good chance the problem will go away.
UPDATE
After further research it looks like pry-remote and pry-byebug do not work well together on Ruby 2.x.
However, there is a bit of code a coder posted, most of the comments are in Japanese, but they claim it makes pry-remote work. I'm including their code below (I've translated the Japanese as well):
module ObjectUtils
# remote parameter is the ip address, default port is 9876
def pry!(remote=nil, port=9876)
$LOAD_PATH.unshift "#{ENV['RUBY_PATH']}/debuger"
require 'pry'
return if __callee__ == :pry1 and !Pry.instance_variable_get(:#pry_is_start)
Pry.instance_variable_set(:#pry_is_start, true)
# if defined? Pry and defined? PryStackExplorer and Pry.config.hooks.hook_exists? :after_session, :delete_frame_manager
# Pry.config.hooks.delete_hook :when_started, :save_caller_bindings
# Pry.config.hooks.delete_hook :after_session, :delete_frame_manager
# end
if remote or ENV['USE_PRY_REMOTE']
if defined? PryNav
if defined? ActionDispatch # rails application.
if Pry.initial_session?
warn '[0m[33mloading pry remote ...[0m'
binding.of_caller(1).remote_pry(remote, port)
end
else
binding.of_caller(1).remote_pry(remote, port)
end
else
require_remote_debugger
binding.of_caller(1).remote_pry(remote, port)
`notify-send -t 5000 -- "exiting pry remote ..."` if find_executable 'notify-send'
end
else
# if these constants are defined then you're already in a debugger session
if defined? PryByebug or defined? PryDebugger or defined? PryNav
if defined? ActionDispatch # rails application.
if Pry.initial_session?
# When sending a new request, it will reset Pry.initial_sesssion? It is true.
# This ensures that, in the same request the debugger will be activated once.
# Only on the next request will it be reinitialized
warn '[1m[33mloading debugger ...[0m'
binding.of_caller(1).pry
end
else
binding.of_caller(1).pry
end
else
require_debugger
binding.of_caller(1).pry
end
end
end
def require_debugger
case RbConfig::CONFIG['ruby_version']
when '2.0.0'...'3.0.0'
require 'pry-byebug'
when '1.9.0'...'2.0.0'
require 'pry-debugger'
end
# to make sure the debugger is loaded
warn '[1m[33mloading debugger ...[0m'
require 'ap'; AwesomePrint.pry!
rescue LoadError
require 'pry-nav'
warn '[1m[33mloading debugger ...[0m'
end
def require_remote_debugger
require 'pry-remote'
require 'pry-nav'
warn '[0m[33mloading pry remote ...[0m'
`notify-send -t 10000 -- "loading pry remote ..."` if system 'which notify-send &>/dev/null'
require 'ap'; AwesomePrint.pry!
end
end
Kernel.send(:include, ObjectUtils)
Object.send(:include, Kernel)
You'll need to call these methods, and might need to tweak them for your usage.
References
https://github.com/Mon-Ouie/pry-remote/issues/58
https://github.com/Mon-Ouie/pry-remote/issues/35#issuecomment-20290739
https://github.com/deivid-rodriguez/pry-byebug/issues/33
https://rubygems.org/gems/pry-byebug
I am attempting to use the roo gem to process .xlsx spreadsheets that are uploaded by an outside party. I'm getting the following error:
LoadError (cannot load such file -- zip/zipfilesystem):
I've found a lot of questions similar to this one (such as cannot load such file -- zip/zip) and I've tried to follow their solutions. So far, to no avail.
I originally required 'roo' in the controller, and after getting this error tried requiring 'zip/zip', 'zip/zipfilesystem', and just 'zip'. None of these seem to fix anything. I've also tried adding :require => 'zip', :require => 'zip/zipfilesystem', :require => 'zip/zip' to the Gemfile, and none of that seemed to change anything. Here is some pertinent code:
in Gemfile:
# for spreadsheet upload management
gem 'roo'
gem 'rubyzip'
gem 'spreadsheet'
gem 'nokogiri'
installed versions:
nokogiri (1.6.0)
roo (1.12.1)
rubyzip (1.0.0)
spreadsheet (0.8.9)
in Controller:
require 'roo'
module BatchOrderProcessing
class DataFilesController < ApplicationController
def create
# some code here ...
when ".xlsx"
spreadsheet = Roo::Excelx.new(uploaded_io.path, nil, :ignore)
header = spreadsheet.row(1)
if # some validation stuff...
puts "spreadsheet format inappropriate"
redirect_to # some place
end
process_datafile(fname, spreadsheet)
# more code ...
end
private
def process_datafile(fname, spreadsheet)
#df = DataFile.new
#df[:filename] = ActiveRecord::Base.connection.quote(fname)
if #df.save
begin
# parse asynchronously
datafile_scheduler = Rufus::Scheduler.new
datafile_scheduler.in '3s' do
#df.process_spreadsheet(spreadsheet)
end
redirect_to #df
rescue => e
# more code ...
end
else
# more code ...
end
end
I think this thing is crapping out before it gets to the model (where the process_spreadsheet() code is), but just in case, here's some model code:
def process_spreadsheet(spreadsheet)
# do some stuff
puts "parsing spreadsheet"
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
row_array << row
invoice << row.to_s
# some more code....
dfi = DataFileItem.new()
dfi.attributes = row.to_hash.slice(*accessible_attributes)
dfi.data_file_id = self.id
dfi.save
self.data_file_items << dfi
# Update stuff in our DB based on rows in row_array...
end
I'm using rails 3.2.13 and ruby 2.0.0p195.
Am I requiring the wrong thing (or in the wrong way) somewhere? Let me know if any other code snippets would be helpful. Thaaaaanks.
rubyzip v1.0.0 was released 29 August 2013: https://github.com/rubyzip/rubyzip/releases
This is a new major version number, and more than one gem or project that depends on this has been caught out by the break with backwards-compatibility.
The quickest "get my code working like before" fix is to alter Gemfile reference to rubyzip:
gem 'rubyzip', '< 1.0.0'
In the longer-term, this may not be the best fix, it depends on how and/or why you are using rubyzip. I expect some gem publishers such as roo's authors will need to figure out how to transition nicely so that their own users don't end up with simultaneous requirements for incompatible versions of rubyzip.
Just opinion:
Seeing this in action has actually made me much less a fan of Ruby gems semantic versioning for major versions. If I ever break with backwards compatibility on any of my own projects, I think I'll just start a new gem, and put a notice on the old gem.
Try adding the zip-zip gem to your project. It provides a simple adapter for your dependencies using the RubyZip v0.9.9 interface allowing you to upgrade to RubyZip v1.0.0.
This has been fixed in the roo gem. You need to update to version 1.12.2 or higher to fix. See the issue here: https://github.com/Empact/roo/pull/65
I'm trying to use the activerecord-import gem and I've followed the directions on the wiki to a tee but I'm getting a NoMethodError: undefined method 'import' for #<Class:0x8b009b0>. Here's my code (basically the same as the example from the wiki)
class ExampleCode
def self.testing
orders = []
10.times do |i|
orders << Order.new(:raw_data => "order #{i}")
end
Order.import orders
end
end
I call the method like so:
ExampleCode.testing
I've tried on windows, linux, with a sqlite database, a mysql database and still no luck. And I'm certain I have the gem installed:
actionmailer (3.2.6, 3.2.3, 3.2.1, 3.2.0)
actionpack (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activemodel (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activerecord (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activerecord-import (0.2.10)
activerecord-oracle_enhanced-adapter (1.4.1)
activerecord-sqlserver-adapter (3.2.1)....
I even tried to use require (which shouldn't be necessary when the gem is installed. I haven't seen this come up anywhere else so I fear I must to missing something very obvious
You'll have to import active_record and activerecord-import
i.e.
require active_record
require activerecord-import
(as mentioned in the wiki)
The reason being, ruby won't know about it unless you explicitly import those libraries. In case of a rails project, rails imports all gems mentioned in the Gemfile for you.
I ran into the same thing; turns out that for me at least I had the gem included within a test block in the gemfile. make sure that when you include it it's not just limited to one app environment in the gem file.