Checking the Rails Console Database Schema

I'm new to rails / ruby, and I was wondering how I can check if my database schema is built correctly in the rails console.

In rails c

ActiveRecord::Base.connection.tables gave me the output

 ["schema_migrations", "users", "expense_pictures", "income_pictures", "income_texts", "expense_texts"] 

How can I verify in the console that I have correctly built the following circuit?

enter image description here

From the user to IncomePictures and ExpensePictures, foreign keys are used. IncomePictures to ExpensePictures are also foreign keys to their respective texts.

Here are my models:

 class ExpensePicture < ActiveRecord::Base belongs_to :user mount_uploader :image, ImageUploader has_one :expense_text end class ExpenseText < ActiveRecord::Base belongs_to :expense_pictures end class IncomePicture < ActiveRecord::Base belongs_to :user mount_uploader :image, ImageUploader has_one :income_text end class IncomeText < ActiveRecord::Base belongs_to :income_pictures end class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :rememberable, :validatable has_many :expense_pictures has_many :income_pictures end 
+6
source share
2 answers

You can check this in the db/schema.rb . If you are not sure, you can run bundle exec rake db:schema:dump earlier - this rake command recreates the schema.rb file from the database.

According to your edited question, you should create the following migrations:

 bundle exec rails g migration add_user_id_to_expense_pictures user:references bundle exec rails g migration add_expense_picture_id_to_expense_texts expense_picture:references bundle exec rails g migration add_user_id_to_income_pictures user:references bundle exec rails g migration add_income_picture_id_to_income_texts income_picture:references 

and run them using bundle exec rake db:migrate .

In addition, you have some of your associations that are set incorrectly. It should be:

 class ExpenseText < ActiveRecord::Base belongs_to :expense_picture end 

and

 class IncomeText < ActiveRecord::Base belongs_to :income_picture end 
+7
source

I think you could visualize Rails schema.rb with some of the tools listed in this link

0
source

Source: https://habr.com/ru/post/972634/


All Articles