답변:
이것은 테이블의 column_names를 나열합니다.
Model.column_names
e.g. User.column_names
Model.columns은 ActiveRecord를 통해 테이블에 대한 모든 정보를 제공합니다. 결정적으로 그것은 데이터베이스 수준에서 내 기본 키가 실제로 무엇인지 확신을 얻을 수있는 유일하고 쉬운 방법 이었습니다.
이것은 열 이름뿐만 아니라 열을 가져오고 ActiveRecord :: Base :: Connection을 사용하므로 모델이 필요하지 않습니다. db의 구조를 빠르게 출력하는 데 편리합니다.
ActiveRecord::Base.connection.tables.each do |table_name|
puts table_name
ActiveRecord::Base.connection.columns(table_name).each do |c|
puts "- #{c.name}: #{c.type} #{c.limit}"
end
end
primary속성이 올바르게 설정되지 않습니다 (모든 열에 primary=nil). Model.columnssrt32에서 제안한 방법으로 올바르게 설정되어 있습니다.
레일 3을 사용하여 모델 이름을 입력 할 수 있습니다.
> User
gives:
User(id: integer, name: string, email: string, etc...)
레일 4에서는 먼저 연결을 설정해야합니다.
irb(main):001:0> User
=> User (call 'User.connection' to establish a connection)
irb(main):002:0> User.connection; nil #call nil to stop repl spitting out the connection object (long)
=> nil
irb(main):003:0> User
User(id: integer, name: string, email: string, etc...)
예를 들어 rails console o rails dbconsole을 사용하여이 유용한 정보를 보완합니다.
학생은 레일 콘솔을 사용하는 내 모델입니다.
$ rails console
> Student.column_names
=> ["id", "name", "surname", "created_at", "updated_at"]
> Student
=> Student(id: integer, name: string, surname: string, created_at: datetime, updated_at: datetime)
Rails를 통해 SQLite를 사용하는 다른 옵션 :
$ rails dbconsole
sqlite> .help
sqlite> .table
ar_internal_metadata relatives schools
relationships schema_migrations students
sqlite> .schema students
CREATE TABLE "students" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar, "surname" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
마지막으로 자세한 내용은.
sqlite> .help
도움이 되었기를 바랍니다!
Model.columns데이터베이스 구성 데이터를 포함하여 열에 대한 자세한 정보를 얻으려면 같은 것을 실행할 수도 있습니다 .