10

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

In rails c

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

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

How can I check within the console that I have the following schema correctly built?

enter image description here

From user to IncomePictures and ExpensePictures are foreign keys. IncomePictures to ExpensePictures are also foreign keys to their appropriate 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
Liondancer
  • 15,721
  • 51
  • 149
  • 255

3 Answers3

9

You can check this in db/schema.rb file. If you aren't sure, you can run bundle exec rake db:schema:dump previously - this rake task recreates schema.rb file from database.

According to your edited question, you should generate 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 with bundle exec rake db:migrate.

also, you have some of your associations set unproperly. It should be:

class ExpenseText < ActiveRecord::Base
  belongs_to :expense_picture
end

and

class IncomeText < ActiveRecord::Base
  belongs_to :income_picture
end
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • I'm pretty new and I feel a bit lost with all teh migration and the magic that rake migrations perform. I currently haev this. I feel as if I am sitll missing something http://dpaste.com/2TKPYPK – Liondancer Jul 22 '14 at 07:17
  • @Liondancer so your current db schema is not what you expect? – Marek Lipka Jul 22 '14 at 07:18
  • Umm im jsut not sure if the foreign key connections are made – Liondancer Jul 22 '14 at 07:19
  • Why do you expect foreign key connections to be made? Rails don't make them by default. – Marek Lipka Jul 22 '14 at 07:20
  • I'm new to rails so I didnt know that. I thought has_many and belongs_to did the trick – Liondancer Jul 22 '14 at 07:22
  • No. Apart from defining `has_many` and `belongs_to`, you should create migration modifying your db schema properly. So please paste in your question what are your models that you want to associate with (or, maybe even better, create another question). – Marek Lipka Jul 22 '14 at 07:23
  • Just updated my question. Thanks for the help. I really appreciate it – Liondancer Jul 22 '14 at 07:27
  • Thank you for your help! If i could give more than one up votes i would! – Liondancer Jul 22 '14 at 07:42
  • Is there a more visual way other than looking at the schema? It's very hard to do when you got multiple tables with a lot of columns. Be good if you can view them all on excel or something. – Ka Mok Jun 25 '16 at 04:10
  • Probably there is, but I don't use it, so I don't know how to. – Marek Lipka Jun 27 '16 at 14:07
4

ActiveRecord::Base.connection.schema_search_path command will help you know the current schema

Realms AI
  • 101
  • 4
0

I think you could visualize Rails schema.rb by some tools listed in this link

Community
  • 1
  • 1
Phan Hai Quang
  • 709
  • 6
  • 7