Skip to content

Commit

Permalink
Merge pull request #200 from straight-shoota/fix/has_one-nilable
Browse files Browse the repository at this point in the history
Fix has_one getter for nilable type
  • Loading branch information
anykeyh authored Nov 30, 2020
2 parents b98e233 + 6d76430 commit 3bdaa44
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions spec/model/relations/fixture.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module RelationSpec
has_one user_info : UserInfo, foreign_key: "user_id"
# Same...
has_many user_infos : UserInfo, foreign_key: "user_id"
has_one user_info_nilable : UserInfo?, foreign_key: "user_id"

has_many posts : Post, foreign_key: "user_id"

Expand Down
6 changes: 3 additions & 3 deletions src/clear/model/modules/relations/has_one_macro.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ module Clear::Model::Relations::HasOneMacro

{% if nilable %}
def {{method_name}}! : {{relation_type}}
if model = self.{{method_name}}.nil?
if model = self.{{method_name}}
model
else
raise Clear::SQL::RecordNotFoundError.new
end

model
end # / *!
{% end %}

Expand Down

0 comments on commit 3bdaa44

Please sign in to comment.