gpt4 book ai didi

ruby-on-rails - Rails 控制台数据库模式检查

转载 作者:数据小太阳 更新时间:2023-10-29 06:53:57 24 4
gpt4 key购买 nike

我是 rails/ruby 的新手,我想知道如何在 rails 控制台中仔细检查我的数据库模式是否正确构建

rails c

ActiveRecord::Base.connection.tables 给了我

的输出
["schema_migrations", "users", "expense_pictures", "income_pictures", "income_texts", "expense_texts"] 

如何在控制台中检查我是否正确构建了以下架构?

enter image description here

从用户到 IncomePictures 和 ExpensePictures 是外键。 IncomePictures 到 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

最佳答案

您可以在 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

关于ruby-on-rails - Rails 控制台数据库模式检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24880956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com