Skip to content

Commit

Permalink
Only define #reload when Spec module is defined
Browse files Browse the repository at this point in the history
  • Loading branch information
stufro committed Oct 17, 2023
1 parent 72b01d4 commit 6935e64
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ post = Post.first! # raises when no records exist
### reload

Returns the record with the attributes reloaded from the database.
**Note:** this method is only defined when the `Spec` module is present.

```
post = Post.create(name: "Granite Rocks!", body: "Check this out.")
Expand Down
18 changes: 14 additions & 4 deletions src/granite/querying.cr
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,18 @@ module Granite::Querying
end
end

# reload returns the record with the attributes reloaded from the database
def reload
self.class.find!(primary_key_value)
end
{% if parse_type("Spec").resolve? %}
# Returns the record with the attributes reloaded from the database.
#
# **Note:** this method is only defined when the `Spec` module is present.
#
# ```
# post = Post.create(name: "Granite Rocks!", body: "Check this out.")
# # record gets updated by another process
# post.reload # performs another find to fetch the record again
# ```
def reload
self.class.find!(primary_key_value)
end
{% end %}
end

0 comments on commit 6935e64

Please sign in to comment.