1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Get The Column Names For A Model as a Rails til

This commit is contained in:
jbranchaud
2019-07-16 14:01:29 -05:00
parent 1da64f9379
commit ced73b4c04
2 changed files with 18 additions and 1 deletions

View File

@@ -0,0 +1,16 @@
# Get The Column Names For A Model
An `ActiveRecord` model is backed by a table in your application's database. We
can get the column names for a backing table with `ActiveRecord`s help using
the `.column_names` method.
```ruby
> Book.column_names
=> ["id", "title", "publication_year", "created_at", "updated_at", "author_id", "genre"]
```
Any `ActiveRecord` model will have access to this class method.
See [the
docs](https://devdocs.io/rails~5.2/activerecord/modelschema/classmethods#method-i-column_names)
for more details.