# Coercing to domain model

**URL:** <https://discourse.dry-rb.org/t/coercing-to-domain-model/69>\
**Category:** dry-types\
**Created:** [July 20, 2016, 12:56pm UTC](https://discourse.dry-rb.org/t/coercing-to-domain-model/69 "2016-07-20T12:56:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![graf0](https://avatars.discourse-cdn.com/v4/letter/g/8c91f0/32.png) [@graf0](https://discourse.dry-rb.org/u/graf0)\
**Post date:** [July 20, 2016, 12:56pm UTC](https://discourse.dry-rb.org/t/coercing-to-domain-model/69/1 "2016-07-20T12:56:14Z")

</div>

Hi,

I’m struggling to solve following problem: client sends uuid of domain model to my app. I’d like to coerce this to AR record (query database for uuid column), or nil - if no record found. How to approch that problem using dry-types? I’ve found a lot of examples how to modify available types, but none how to make your own type from scratch. Can you help with some example?

Greg

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [July 21, 2016, 4:40pm UTC](https://discourse.dry-rb.org/t/coercing-to-domain-model/69/2 "2016-07-21T16:40:03Z")

</div>

Hey,

I don’t think that this is a good use case for a dry type object **but** this could be done like that:

```ruby
module Types
  include Dry::Types.module

  SomeModelFromUUID = Dry::Types::Definition.new(SomeModel).constructor do |uuid|
    SomeModel.find_by(uuid: uuid)
  end
end

Types::SomeModelFromUUID["04953F81-4621-423F-892A-07CAB33B47E9"]
# returns SomeModel instance or nil

```

Hope this is helps  
Cheers 🙂
