我是rails / ruby的新手,我想知道如何仔细检查我的数据库架构是否在rails控制台中正确构建
在rails c
ActiveRecord::Base.connection.tables
给了我
["schema_migrations", "users", "expense_pictures", "income_pictures", "income_texts", "expense_texts"]
如何在控制台中检查我是否正确构建了以下架构?
从用户到IncomePictures和ExpensePictures是外键。 IncomePictures to ExpensePictures也是其相应文本的外键
以下是我的模特:
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
答案 0 :(得分:7)
您可以在db/schema.rb
文件中查看此内容。如果您不确定,可以先运行bundle exec rake db:schema:dump
- 此rake任务会从数据库重新创建schema.rb
文件。
根据您编辑的问题,您应该生成以下迁移:
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
并使用bundle exec rake db:migrate
运行它们。
另外,您的某些关联设置不正确。它应该是:
class ExpenseText < ActiveRecord::Base
belongs_to :expense_picture
end
和
class IncomeText < ActiveRecord::Base
belongs_to :income_picture
end
答案 1 :(得分:0)
我认为你可以通过this link
中列出的一些工具来可视化Rails schema.rb答案 2 :(得分:0)
ActiveRecord::Base.connection.schema_search_path
命令将帮助您了解当前架构