How to use Rails with Mongoid and not PostgreSQL or SQLite? - ruby-on-rails

Added Mongoid but got:
LoadError:
Error loading the 'sqlite3' Active Record adapter.
Removed database.yml and got:
Could not load database configuration. No such file - ["config/database.yml"]
Saved empty database.yml and got:
ActiveRecord::AdapterNotSpecified:
'test' database is not configured. Available: []
Rails 5.2.2

If it is fresh install use
rails new app --skip-active-record
if you already created an App with rails, which has active-record(mysql or sqlite3)
use this link:
Rails with Mongoid
Also remove
active records
lines from
application.rb

ActiveRecord is a library that works only with relational databases. If you want to use MongoDB with mongoid, you don't need ActiveRecord. Remove any mention of AR and relational DBs from the application code, config and Gemfile. If you're stating from scratch, use rails new myapp --skip-active-record
See also: https://gorails.com/guides/setting-up-rails-4-with-mongodb-and-mongoid (related to Rails 4, but should help).

Related

Adding ActiveRecord on top of a Rails + Mongoid install

I have an existing project with Rails 5.2 and Mongoid 7, I had previously disabled all activerecord-related modules.
We are working on some synchronization with business intelligence data warehouses, and I discovered this gem that seems like a good starting point : I'm planning to use ActiveRecord with this adapter to easily implement code that will push data to my Amazon Aurora DB for BI purposes.
I have created a simple model
# model/test.rb
class Test < ActiveRecord::Base
end
I have added a database.yml
# config/database.yml
development:
adapter: aurora_serverless
...
But when I try to do anything with a model it says it could not connect
ActiveRecord::ConnectionNotEstablished: No connection with pool with 'primary' found
Am I missing other things to make ActiveRecord work ? Do I need some additional Railties, etc ? It's as if the database.yml file was not read at all
Here is a summary of the things I ended up doing
add required gems in your Gemfile (as a matter of fact, the gem I added did not even require the mysql2 gem which is quite a relief as mysql2 has dependencies on the local OS libs, and you're quite likely to encounter many bugs)
create a config/database.yml file, and have one key per environment you need, with the AR config you need
in application.rb, add require 'active_record/railtie' (the railtie will initialize connection from the database.yml file)
create some models
Maybe you need extra adapters for local/test/staging/prod environments
Add a container for a SQL DB if you're working with docker-compose

Rails 5 project without activerecord causing model generate error

I'm facing a problem in a project using Rails 5.2 without ActiveRecord.
I've runned the command rails new project --skip-active-record and it's ok, but when I run a command to generate a model I get this error:
.rvm/gems/ruby-2.5.1/gems/bundler-1.16.1/lib/bundler/rubygems_integration.rb:404:in
`block (2 levels) in replace_gem': Error loading the 'sqlite3' Active
Record adapter. Missing a gem it depends on? sqlite3 is not part of
the bundle. Add it to your Gemfile. (Gem::LoadError)
I didn't understand why it ask about sqlite 3 in active record if I disabled it and I search over internet and don't find anyone talking about.
What is wrong?
Thanks
If you don't want to use ActiveRecord then there's no need to generate a model using rails g.
You can just go to the app/models directory in your application and create a plain ruby class to work with it as a model.
The command
rails g model MODEL
will generate model, migration and fixtures. And this is related to ORM. So rails will look at database configuration. There you would have mentioned the ORM client as
adapter: sqlite3
You need to install sqlite3 gem for this. Add
gem 'sqlite3'
to your Gemfile
If you want to add just model alone create file inside app/models folder. But whats the point in doing this? Rails will look for the table name respect to the model's class name and throw an error
I assume you don't have an ORM. If that is the case then running rails generate model shouldn't do anything, it shouldn't even install sqlite by default. Try updating your sqlite gem
The $ bin/rails generate model ... command actually creates a migration and an ActiveRecord model that's why you get the database-related error. See Model Generators.

I am using rails-api and I my rake db:seed command doesn't seem to work, what has been removed that contained this functionality?

This is my Gemfile:
this is the schema
this is the seed file (I am aware of the semicolon typo)
I am new to rails and not exactly sure what the function of all bundled gems were so I was wondering which gem seeded the database? Or why the way I wrote my seed.rb file is causing a rake aborts.

is it possible to add migration for rails app to ruby gem?

For my rails project I want to write a ruby gem which have an ActiveRecord model eg. Animal < ActiveRecord::Base. Is it possible to add migration cretae_animals to the gem in such a way that when the gem installed in my app and I run rake db:migrate that migration will execute?
You can do this by including the migrations with your gem, as well as including a rake task that runs them. Then you call the rake task as follows:
myGem = Gem::Specification.find_by_name 'gem-name'
load "#{myGem.gem_dir}/lib/tasks/my_migration.rake"
(Proper credit to Andy Atkinson, where I originally learned this for a similar project.)

Smartquotes displayed fine in Rails 2, now a problem in Rails 3

My Rails app is working from a mysql database, and when I switched from Rails 2.8, Ruby 1.8 and the mysql gem over to Rails 3.0.7, Ruby 1.9.2 and the mysql2 gem, all of a sudden my pages are rendering with smartquotes (curly quotes) and em-dashes, etc., all looking like gibberish.
I'm assuming this has something to do with UTF-8, but I don't know how to pinpoint it.
Here's what I do know:
(1) config/database.yml has the following:
development:
adapter: mysql2
encoding: utf8
(2) config/application.rb has the following:
config.encoding = "utf-8"
Don't know where to go from there.
Any suggestions?
You can narrow it down to the db vs the view by doing two things:
rails console # in your terminal in the project directory, then test that your ActiveRecord objects have the correct data.
ViewSource in your browser to see if the generated source is putting in the correct characters.
Later:
This is probably a duplicate of: mysql2 gem, Rails 3.0.3 and "incompatible character encodings" errors https://github.com/brianmario/mysql2/issues/124
And maybe even the monkeypatch here: http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/

Resources