Adding ActiveRecord on top of a Rails + Mongoid install - ruby-on-rails

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

Related

Can't mount newly generated Rails engine in an app

I have a complex Rails app and I want to extract some core functionality into an engine so that I can reuse the models etc in other Rails apps.
I've been following the official documentation for engines (https://guides.rubyonrails.org/engines.html). I'm able to create a new engine inside the app and generate some test models
> rails plugin new testengine --mountable
testengine> rails generate model Test
This is the .gemspec
require_relative "lib/testengine/version"
Gem::Specification.new do |spec|
spec.name = "testengine"
spec.version = Testengine::VERSION
spec.authors = ["Me"]
spec.summary = "testengine"
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
spec.files = Dir["{app,config,db,lib}/**/*", "Rakefile", "README.md"]
spec.add_dependency "rails", "~> 6.1.4"
end
I console into the test dummy rails app in testengine, and I can find my new model at Testengine::Test, no problem. So far so good.
Now I get to section 4.1 Mounting the Engine. I add the engine via the Gemfile file (in fact this is already done for me thanks to the rails generator above).
gem 'testengine', path: 'testengine'
Then I install my gems without problems.
> bundle install
...
Using testengine 0.1.0 from source at `testengine`
...
I console into the main app and I can find Testengine and Testengine::VERSION but not Testengine::Engine or Testengine::Test.
Reading a little further the docs say you need add this line to config/routes.rb
mount Testengine::Engine, at: "/testengine"
I do and now the rails app won't even start
config/routes.rb:3:in `block in <top (required)>': uninitialized constant Testengine::Engine (NameError)
What did I miss?
I will answer my question for the benefit of others who might make the same mistake I made. In my case, gem 'testengine', path: 'testengine' was buried inside a group of gems e.g. in
group :test do
...
end
I guess I was confused how rails loads gems from groups and missed the detail about group inclusion. It seems that while it'll show in the list during bundle install, and autoload some basic items, such as Testengine::VERSION it doesn't autoload everything unless you are running an environment as the same name as the group. In hindsight, this seems a bit obvious. Lesson learned.

How to use Rails with Mongoid and not PostgreSQL or SQLite?

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).

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.

Ruby on Rails: Best way to add gems at project initialization

Every month or so I have to create a Rails project at work.
The procedure is always the same...
Run rails new [...] & edit the Gemfile.
I have about 12 gems I always add on any project no matter what kind of project that is. Most of them are development gems, some of them production gems.
The gems range from ActiveAdmin to BetterErrors and so on. Problem is: I always have to remember which gems I use on a regular basis and run the same procedure everytime I start a project:
Open google.com
Search for the correct names of the gems
Add the gems to the Gemfile accordingly
Repeat til I'm satisfied with my Gemfile
I wish I could do something like this: rails new Project --use-my-gems
I know it's possible somehow using a text-file to store my most used gems (or a Gemfile itself and overwrite the default Gemfile with my).
Basically I want multiple Rails Gemtemplates which can be switched at project initialization by passing an argument to rails.
Use Rails Templates.
You can specify your gems in the template:
gem "bj"
gem "nokogiri"
... with a lot of other options.
Then start your new app with:
rails new app -m ~/template.rb

Use two different databases in one project

Is it possible to use two different databases for the same project in Ruby On Rails?
At the moment I use PostgreSQL with Heroku for my project and I would like to use Redis to store some of my models.
Thanks
First step, add redis-rb to your Gemfile:
gem 'redis', '2.1.1'
Then install the gem via Bundler:
bundle install
Lastly, create an initializer in config/initializers/redis.rb and add the following:
$redis = Redis.new(:host => 'localhost', :port => 6379)
Will that have any side effects to my existing database PostgreSQL database?
Or I 'll able to use $redis whenever I want to store something?
If you follow the steps you've written, you shouldn't have any problems with ActiveRecord/Postgres
$redis will work fine for you, as long as the connection works.
If you want to use Redis as a database storage for your model then you should use some of the available gems that lets you easily store objects in Redis. Some of the gems are:
OHM
Redis::Objects

Resources