Skip to content

Commit

Permalink
mention _for_database and _in_database methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jrochkind committed Dec 22, 2022
1 parent a7388e5 commit 63d93c9
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,9 @@ You can use ordinary ActiveRecord validation methods with `attr_json` attributes
All the `attr_json` attributes are serialized to json as keys in a hash, in a database jsonb/json column. By default, in a column `json_attributes`.
If you look at `model.json_attributes`, you'll see values already cast to their ruby representations.

But one way to see something like what it's really like in the db is to
save the record and then use the standard Rails `*_before_type_cast` method.
To see JSON representations, we can use Rails [\*\_before_type_cast](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/BeforeTypeCast.html) methods, [\*\-in_database](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Dirty.html#method-i-attribute_in_database) and [\*\_for_database] methods (Rails 7.0+ only).

```ruby
model.save!
model.json_attributes_before_type_cast
# => string containing: {"my_integer":12,"int_array":[12],"my_datetime":"2016-01-01T17:45:00.000Z"}
```
These methods can all be called on ont the container `json_attributes` json hash attribute (generally showing serialized JSONto string), or any individual attribute (generally showing in-memory JSON-able object). [This is a bit confusing and possibly not entirely consistent, needs more investigation.]

## Specifying db column to use

Expand Down

0 comments on commit 63d93c9

Please sign in to comment.