mirror of
https://github.com/jbranchaud/til
synced 2026-01-04 23:58:01 +00:00
Add Get The Column Names For A Model as a Rails til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_824 TILs and counting..._
|
_825 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -499,6 +499,7 @@ _824 TILs and counting..._
|
|||||||
- [Disambiguate Where In A Joined Relation](rails/disambiguate-where-in-a-joined-relation.md)
|
- [Disambiguate Where In A Joined Relation](rails/disambiguate-where-in-a-joined-relation.md)
|
||||||
- [Ensure Migrations Use The Latest Schema](rails/ensure-migrations-use-the-latest-schema.md)
|
- [Ensure Migrations Use The Latest Schema](rails/ensure-migrations-use-the-latest-schema.md)
|
||||||
- [Generating And Executing SQL](rails/generating-and-executing-sql.md)
|
- [Generating And Executing SQL](rails/generating-and-executing-sql.md)
|
||||||
|
- [Get The Column Names For A Model](rails/get-the-column-names-for-a-model.md)
|
||||||
- [Hash Slicing](rails/hash-slicing.md)
|
- [Hash Slicing](rails/hash-slicing.md)
|
||||||
- [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md)
|
- [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md)
|
||||||
- [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md)
|
- [List The Enqueued Jobs](rails/list-the-enqueued-jobs.md)
|
||||||
|
|||||||
16
rails/get-the-column-names-for-a-model.md
Normal file
16
rails/get-the-column-names-for-a-model.md
Normal 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.
|
||||||
Reference in New Issue
Block a user