簡體   English   中英

在 rails 中查看 schema.rb 中表的內容

[英]Viewing the contents of tables in schema.rb in rails

如果這是一個愚蠢的問題,我很抱歉,但是在我的 schema.rb 中,我有幾個表,例如

  create_table "messages", :force => true do |t|
    t.integer  "user_id",            :null => false
    t.string   "message",            :null => false
    t.datetime "created_at",         :null => false
    t.string   "photo_file_name"
    t.string   "photo_content_type"
    t.integer  "photo_file_size"
    t.datetime "photo_updated_at"
  end

是否可以查看每個表的內容,即查看每條消息和相關的用戶 ID、消息內容、創建時間、鏈接圖像等?

謝謝

數據庫模式表示數據庫的結構,而不是它的內容。

如果您想訪問數據庫中的內容,您可以查詢它來執行此操作。 您可以通過命令行客戶端(運行$ rails dbconsole將嘗試為配置的數據庫打開一個)或 Sequel Pro 之類的圖形工具(適用於 Mac OS X 上的 MySQL)來執行此操作。

您也可以通過運行$ rails console ,然后使用 ActiveRecord 提供的方法(例如Post.allUser.where(:name => 'Billy').limit(5) )通過 Rails 應用程序獲取此信息。

您可以使用gem "annotate" 這對我來說是非常有幫助的。

在 Rails 控制台上查看表結構只需要運行表名,例如如果有一個名為 country 的表。 只運行“國家”

2.4.4:004 > Country => Country(id: integer, name: string, percent: string, created_at: datetime, updated_at: datetime, is_sync: boolean) 2.4.4:005 >

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM