How can re-create the db file in ruby on rails - ruby-on-rails

When I create an app by rails new I skip the few options which I never used.
rails new backend --api --skip-action-mailer --skip-action-mailbox --skip-active-storage --skip-action-cable --skip-javascript --skip-test --database=postgresql
...but I realized I can not find my .db file. Is this because I skip the test or action-storage? I suddenly think I should not use --skip options if it is not clear idea...

Based on you question it sounds like you have not run rails db:create at the command line yet. You must do this once your Rails project has been created in order to access the database.

Postgres stores data in own folder, only sqlite stores data in your project folder.
For database configuration check config/database.yml

Related

Rails: conflict error overwrite /home/username/blog/.gitignore?

I'm desperately trying to run a server and keep getting this mistake: conflict .gitignore
The output is:
rails new blog
exist
identical README.md
identical Rakefile
identical .ruby-version
identical config.ru
conflict .gitignore
Overwrite /home/jules/blog/.gitignore? (enter "h" for help) [Ynaqdhm]
What should I do? I'm using Ubuntu 20.04. I'm an absolute beginner.
When you run command $rails new it tries to create new rails application for you. But inside that folder that you are running this command file .gitignore already exists.
So, all that you need is to decide do you want to use your .gitignore file that already exists in current folder or to overwrite it with default one.
I think you made some mistake and just to make things simplier, try to run your command rails new blog in some separate empty folder.
It seems like you run rails new blog instead of rails s/rails server.
Since it already has all the files of a rails application called "blog", it's asking if it should overwrite the existing files with the new files it is creating because you ran rails new blog.

Run ruby file in rails console

I've created a file ebm.rb script to add new entries to a database in rails.
The file is very long, and uses some rails models.
Can I easily execute ebm.rb in the rails console?
I tried something with load and require, but that didn't work. My ebm.rb file is located in C:\Sites\ebm and my rails project in C:\Sites\rublesql.
You can run the code in the file in the context of your rails app with
rails runner
http://guides.rubyonrails.org/command_line.html#rails-runner

How to create database from schema.rb without initializing Rails?

I am trying to create all my tables from schema.rb
I used the command: "rake db:schema:load"
However, this fails because in one of my initializers, it is referencing a model/table that obviously doesn't exist (since the database is empty)
I could comment out these lines, and then run schema:load again, but is there an alternative?
Probably the fastest way is to just move the offending initializer to a temporary directory that is outside of the app, and then run your schema load. But if that doesn't work, or isn't an option for some reason, you could always work around that by creating a bare bones rails app to do the schema load:
Create a new rails app: rails new (app)-fixer
Copy your gemfile (unless there are specific exceptions) to the fixer app.
Copy your database.yml config to the fixer app.
Copy your schema.rb file to the fixer app.
Do all appropriate "bundle install" commands as needed for your app.
Then run "rake db:drop db:create db:schema:load"
That will build up a new database from scratch, based on your current schema.
You can add a check for the table existance in your initializer.
if TheModel.table_exists?
// do something with the model
end

How to load the data from a .sqlite3 file to a Ruby on Rails Application

I have a production.sqlite3 file which I want to import its data to the current rails project, the database schema is match between the file and my current project. I did copy the content to the development.sqlite3 file but this does not work. The only way I know to insert data to the database is by loading some yml file or use the seed command. Is there any magic command or other ways to let the rails loading the data from .sqlite file? Because from what I disover, the behavior of rails is that it only creates the .sqlite3 file when you do rake db:create &migrate, but it will ignore what ever manual changes happen in that file. Please help!
It looks like renaming your .sqlite3 file should work. Make sure you have restarted your server, and that the server is running in the correct environment. If you move both the .sqlite3 files into a completely different folder, and restart the server, an error should occur. If it doesn't, your server didn't properly restart - kill all ruby processes and try again.
In answer to your question about migrating data, I had a similar problem recently, where I had to migrate from sqlite3 to mysql. Best solution I found was yaml_db. It seemed to do the job beautifully - add it to the project, then do
rake db:data:dump
Swap the database configurations (or migrate a new database file, or change your development environment, whatever it is you need to do to make an empty but structured database active), then:
rake db:data:load

Possible to 'copy and paste' a whole Rails application?

I have a fully-functioning Rails application running on my local machine called 'first-app' in my Rails applications folder 'rails-apps'. I would like to create a second application in rails-apps (called 'second-app') which is identical to first-app, just with a different name.
Can I simply copy-and-paste first-app and rename the folder to 'second-app'? I have found only two files in the whole application which contain the term 'first-app' (application.rb and routes.rb, both in config), so presumably I would have to change their contents as well. If this 'copy-and-paste' approach is viable, are there any other files I would have to alter?
If I have to do the usual > rails new second-app, I must be able to copy and paste a lot of the files and folders from first-app. Which are the ones that I have to manually alter or construct with a rails command?
yes, it should works! Just replace in the copy-project the old terms('first-app') to 'second-app' and create a new database for the new app.
You can copy and past the first app folder and use the gem 'rename' to rename the copied app.
Follow these instructions: How to rename rails 4 app?
Just copy your app to a new folder :
$ cp your-old-app your-new-app
If you want to create a new database for the new app, in your config/database.yml rename the development database.
After that, all you have to do is
rake db:create db:migrate

Resources