ruby 在Rails中查看schema.rb中表的内容

omjgkv6w  于 2023-08-04  发布在  Ruby
关注(0)|答案(4)|浏览(121)

如果这是一个愚蠢的问题,我很抱歉,但在我的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、消息内容、创建时间、链接图像等?
谢啦,谢啦

wribegjk

wribegjk1#

database schema表示数据库的结构,而不是内容。
如果你想访问数据库中的内容,你可以通过查询来实现。您可以通过命令行客户端(运行$ rails dbconsole将尝试为配置的数据库打开一个)或像Sequel Pro这样的图形化工具(用于Mac OS X上的MySQL)来实现这一点。
您也可以通过Rails应用程序运行$ rails console,然后使用ActiveRecord提供的方法(例如,Post.allUser.where(:name => 'Billy').limit(5))。

v8wbuo2f

v8wbuo2f2#

可以使用gem "annotate"。这对我很有帮助。

eoxn13cs

eoxn13cs3#

你可以使用modelname.all,例如:Country.all将给予表中的所有条目

cnwbcb6i

cnwbcb6i4#

要查看rails控制台上的表结构,只需要运行表名,例如,如果有一个表名为country。只管理“国家”
2.4.4:004 > Country => Country(id:整数,名称:字符串,百分比:string,created_at:datetime,updated_at:datetime,is_sync:boolean)2.4.4:005 >

相关问题