rails and mongodb - ruby-on-rails

I am trying out mongodb with Rails 3. after following instructions from mongomapper's site and a few others, i haven't been able to solve one small issue...
No value provided for required options '--orm'
I added a file mongo.rb in my config folder to make stuff tick
MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
MongoMapper.database = "cobboc_#{Rails.env}"
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
MongoMapper.connection.connect if forked
end
end

The mongo.rb file should be in config/initializers and contain:
require 'mongo_mapper' # loading mongo_mapper
MongoMapper.connection = Monog::Connection.new # localhost and port 27017 are the default values
MongoMapper.database = "cobboc_#{Rails.env}"
The Passenger extension is already done in the MongoMapper code.
If you'd like to use the database.yml file for configuration you can do:
require 'mongo_mapper'
db_config = YAML::load(File.read("#{Rails.root}/config/database.yml"))
if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
mongo_config = db_config[Rails.env]
MongoMapper.connection = Mongo::Connection.new(mongo_config['host'])
MongoMapper.database = mongo_config['database']
end

The project rails3-generators provides MongoMapper model generators to solve your issue. Require the gem in your Gemfile.
# Gemfile
gem 'rails3-generators'
Note, the Rails 3 generators have moved to the mongo_mapper gem

You didn't specify where you were getting the "orm" error
If it was in the "generate model" case you could call the following:
sudo gem install rails3-generators
rails generate model Book --skip-migration --orm=mongomapper

I was running:
$ rails generate scaffold project name:string
>> No value provided for required options '--orm'
Solution:
Add rails3-generators to Gemfile
$ rails g scaffold project name:string --skip-migration --orm=mongomapper

Related

How to create an inline / minimal Rails app?

It's possible, for example, to use ActiveRecord inline in a Ruby script. I like this a lot to report bugs, test features and share gists.
I'm wondering if the same could be done for a Rails webservice? (I'm mainly interested in getting the controller layer to work, the rest should be easy to add on demand.) Something along these lines:
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require 'rails/commands'
APP_PATH = File.expand_path('config/application', __dir__)
Rails::Command.invoke('server')
When toying around with this it did seem that an external entry point (APP_PATH) is required. So alternatively, an acceptable approach would be to cram all config into the single entry point. I couldn't get this to work so far.
The Rails Initialization Process is the best resource I found about this so far.
I produced a minimal Rails app to get started as follows:
rails new min-rails --skip-keeps --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-record --skip-active-storage --skip-puma --skip-action-cable --skip-sprockets --skip-spring --skip-listen --skip-javascript --skip-turbolinks --skip-test --skip-system-test --skip-bootsnap --api
cd min-rails
rm -rf app/jobs app/models config/initializers config/locales lib log public tmp vendor config/environments/test.rb config/environments/production.rb config/credentials.yml.enc config/master.key bin/rake bin/setup bin/bundle
I ended up with the following script:
inline-rails.rb
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require "action_controller/railtie"
class App < Rails::Application
routes.append do
get "/hello/world" => "hello#world"
end
config.consider_all_requests_local = true # display errors
end
class HelloController < ActionController::API
def world
render json: {hello: :world}
end
end
App.initialize!
Rack::Server.new(app: App, Port: 3000).start
Run it as:
ruby inline-rails.rb

How to install migration in Rails engine from other engine?

I have an engine, that has gem dependency. This gem has rake task to install migrations:
rake acts_as_taggable_on_engine:install:migrations
What is the proper way to install migration? When I run this command from host app or my engine I getting
Don't know how to build task
Add the gem dependency to you gemspec:
Gem::Specification.new do |s|
# ...
s.add_dependency 'acts-as-taggable-on', '~> 6.0'
# ...
end
Then require the gem in your engine:
# lib/my_engine/engine.rb
require 'acts-as-taggable-on'
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace Chatty
end
end
ActsAsTaggableOn should be loaded through the main file which requires the engine as well unlike some gems where you require gemname/engine - and the file naming is not snake_case like most gems.
Then run bundle install and rake acts_as_taggable_on_engine:install:migrations in the folder of the dummy application (or the host).
max#MaxBook ~/p/c/t/dummy> rake acts_as_taggable_on_engine:install:migrations
Copied migration 20181030123059_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123060_add_missing_unique_indices.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123061_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123062_add_missing_taggable_index.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123063_change_collation_for_tag_names.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
Copied migration 20181030123064_add_missing_indexes_on_taggings.acts_as_taggable_on_engine.rb from acts_as_taggable_on_engine
I don't know why but invoking the command through bundler (bundle exec ...) does not work. This may give problems with RVM if you are using shims.
You can also create a generator for your engine that invokes the task:
# lib/generators/my_engine/install/install_generator.rb
module MyEngine
class InstallGenerator < Rails::Generators::Base
source_root File.expand_path('templates', __dir__)
desc "Installs MyEngine"
def copy_initializer
# template 'my_engine.rb', 'config/initializers/my_engine.rb'
rake "acts_as_taggable_on_engine:install:migrations"
end
end
end
Which you can then run with rails g my_engine:install.

uninitialized constant ActiveRecord::SessionStore with gem 'activerecord-session_store'

We are upgrading Rails 3.2 engine to Rails 4.2.0 on ruby 2.0.0. Gem 'activerecord-session_store' was added to engine's gemspec following gem's instruction:
s.add_dependency 'activerecord-session_store'
and added following in initializers/session_store.rb under dummy:
Dummy::Application.config.session_store :active_record_store, :key => '_my_app_session'
then, we did bundle install. When we ran:
bundle exec rails generate active_record:session_migration
There is the error from the gem's generator:
/activerecord-session_store-0.1.1/lib/generators/active_record/session_migration_generator.rb:16:in `session_table_name': uninitialized co
nstant ActiveRecord::SessionStore (NameError).
We move the gem into engine's Gemfile and same error. Why the SessionStore is still not initialized?
Update
In engine's engine.rb under lib, the session table is pointed to:
initializer "Authentify.add_middleware" do |app|
ActiveRecord::SessionStore::Session.table_name = 'authentify_sessions'
app.middleware.use ActiveRecord::SessionStore
end
The setup works for Rails 3.2.
If you were not using the default table name for sessions, set:
ActiveRecord::SessionStore::Session.table_name = 'your_old_session_table'
in config/application.rb.
Additional configuration.
What we did is to add gem 'activerecord-session_store' to engine's Gemfile in addition to the .gemspec. The error disappeared.

Install omniauth rails 2.3.4

I have a rails 2.3.4 app that I'd like to extend with omniauth (0.1.5). When I install omniauth gem using rvm and place require 'omniauth' in the config.rb file I get the following error:
`gem_original_require': no such file to load -- omniauth (MissingSourceFile)
The tutorials suggest using putting it in the gemfile but I am using rails 2.
When I 'gem list' omniauth is available however.
This has taken a couple of (hair-pulling) days and I am not sure how to proceed.
Am I placing the require in the correct place or is there somewhere else I could put it (aside form the obvious :-))?
Any ideas would be great....
EDIT 1: I tried config.gem "omniauth" in your environments.rb file and got
/home/mcaulejj/explorer/config/environment.rb:10: undefined local variable or method `config' for main:Object (NameError)
EDIT 2: Using RVM I updated all gems but I am still getting the same error.....
I'm exasperated at this point.
Cheers Slothihtype
Try config.gem "omniauth" in your environments.rb file.
EDIT
As per comment,
try:
require File.join(File.dirname(__FILE__), 'boot')
#insert the following here, in your config/environment.rb
if Gem::VERSION >= "1.3.6"
module Rails
class GemDependency
def requirement
r = super
(r == Gem::Requirement.default) ? nil : r
end
end
end
end
Add require 'oa-oauth' in your environment.rb file

How to configure Ruby on Rails with no database?

It would be convenient to use Ruby on Rails for a small website project that has no current need for a database. I know I could create an empty database in MySQL and go from there, but does anyone know a better way to run Rails without a database?
Thanks
For Rails 3 and Rails 4:
Use -O(Capital 'O') or --skip-activerecord option to generate an application without a database.
rails new myApp -O
or
rails new myApp --skip-activerecord
This Answer is reshared from here
For Rails 5:
Use --skip-active-record option to generate an application without a database
Notice the extra hyphen '-' as opposed to previous Rails versions.
rails new myApp --skip-active-record
For an existing Rails 4-7 project, in your config/application.rb file you have the following line:
require 'rails/all' # or `require "rails"' in newer versions
(As reference that line is loading this file)
So instead of load ALL, you must to load each library separately as follows:
# active_record is what we're not going to use it, so comment it "just in case"
# require "active_record/railtie"
# This is not loaded in rails/all but inside active_record so add it if
# you want your models work as expected
require "active_model/railtie"
# And now the rest
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "active_job/railtie" # Only for Rails >= 4.2
require "action_cable/engine" # Only for Rails >= 5.0
require "sprockets/railtie" # Deprecated for Rails >= 7, so add it only if you're using it
require "rails/test_unit/railtie"
# All these depend on active_record, so they should be excluded also
# require "action_text/engine" # Only for Rails >= 6.0
# require "action_mailbox/engine" # Only for Rails >= 6.0
# require "active_storage/engine" # Only for Rails >= 5.2
Keep an eye to the comments to know what to load regarding your Rails version.
Also check the following files (in case you have them) and comment the following lines:
# package.json
"#rails/activestorage": "^6.0.0",
# app/javascript/packs/application.js
require("#rails/activestorage").start()
# bin/setup
system! 'bin/rails db:prepare'
# config/environments/development.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.migration_error = :page_load
config.active_record.verbose_query_logs = true
# config/environments/test.rb
config.active_storage.service = :test # For Rails >= 5.2
# config/environments/production.rb
config.active_storage.service = :local # For Rails >= 5.2
config.active_record.dump_schema_after_migration = false
# spec/rails_helper.rb
ActiveRecord::Migration.maintain_test_schema!
# test/test_helper.rb
fixtures :all # In case you're using fixtures
# Only for Rails >= 5.0
#config/initializers/new_framework_defaults.rb
Rails.application.config.active_record.belongs_to_required_by_default = true
Also remove any reference to ActiveRecord::Base in your model files (or simply delete the files if apply). For example, the autogenerated app/models/application_record.rb file.
Uncomment this line in the environment.rb file:
config.frameworks -= [ :active_record, :active_resource, :action_mailer]
In Rails 4 when starting a new project you can use -O or --skip-active-record
rails new my_project -O
rails new my_project --skip-active-record
If you've already created a project you will need to comment
require "active_record/railtie"
from config/application.rb and
config.active_record.migration_error = :page_load
from config/environments/development.rb
If you don't need a database then you probably don't need to have the bulk of Rails. You may want a smaller more customizable framework to work with.
Sinatra is a tiny framework that is great for serving up basic static pages.
But if you insist on using Rails here is an article that will show you how to do just that or here.
For support Rails 6 rc1 and activerecord-nulldb-adaptergem we need a monkey patching
In config/initializers/null_db_adapter_monkey_patches.rb
module ActiveRecord
module ConnectionAdapters
class NullDBAdapter < ActiveRecord::ConnectionAdapters::AbstractAdapter
def new_table_definition(table_name = nil, is_temporary = nil)
TableDefinition.new(table_name, is_temporary)
end
end
end
end

Resources