2014-07-22 52 views
6

我是新來的鐵軌/紅寶石,我想知道我怎麼能仔細檢查,如果我的數據庫架構正確建立在軌控制檯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 

回答

7

您可以db/schema.rb文件檢查。如果您不確定,則可以先運行bundle exec rake db:schema:dump - 此耙路任務從數據庫重新創建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 
+0

列出了一些工具,可視化的Rails schema.rb我很新,我覺得有點用全部丟失遷移和掠奪遷移執行的魔力。我現在有這個。我覺得,如果我sitll缺少的東西http://dpaste.com/2TKPYPK – Liondancer

+0

@Liondancer所以你當前的數據庫架構是不是你所期望的? –

+0

Umm im jsut不知道是否有外鍵連接 – Liondancer