How do I list out the column names and field types of an existing table in ruby on rails?
Assume: The app name is "demo2" The model was made off of a scaffold generation ("post")
Extra specifics in regards to which sub-folder I need to change to on the com开发者_StackOverflow中文版mand line would be helpful.
Thanks!
You can do: Post.columns
and then iterate on the resulting Array.
See more methods at Class: ActiveRecord::Base
Post.columns.each do |column|
puts "name: #{column.name}, type: #{column.type}"
end
in rails 4 its:
Post.column_names # => ["id", "title", ... ]
精彩评论