# How to create Entity programmatically with relations?

**URL:** <https://discourse.rom-rb.org/t/how-to-create-entity-programmatically-with-relations/498>\
**Category:** Uncategorized\
**Created:** [January 27, 2022, 3:28pm UTC](https://discourse.rom-rb.org/t/how-to-create-entity-programmatically-with-relations/498 "2022-01-27T15:28:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wuarmin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/wuarmin/32/233_2.png) [@wuarmin](https://discourse.rom-rb.org/u/wuarmin)\
**Post date:** [January 27, 2022, 3:28pm UTC](https://discourse.rom-rb.org/t/how-to-create-entity-programmatically-with-relations/498/1 "2022-01-27T15:28:26Z")

</div>

Hey,

I need to create an Entity programmatically, but I have issues.

I have an entity `Booking (id, name, area)` which holds an `Area (id, name)` and I need to create this Bookings from hashes:

```ruby
# relation
  module Relations
    class Bookings < ROM::Relation[:sql]
      schema(:cost_center_bookings, infer: true) do
        associations do
          belongs_to :area
        end
      end
    end
  end

# entity
module Entities
  class Booking < ROM::Struct
  end
end

module Entities
  class Area < ROM::Struct
  end
end

```

```ruby
hash = { id: 1, name: 'test', area: Area.new(id: 2, name: 'area1')

BookingsRepo.bookings.mapper.model.new(hash)
# returns { id: 1, name: 'test' }, the area is lost!

```

How is it possible to create a Booking-entity holding the area programmatically?

I tried to add the Area attribute explicitly:

```ruby
module Entities
  class Booking < ROM::Struct
    attribute :area, Entities::Area.optional
  end
end

```

After that following works:

```ruby
BookingsRepo.bookings.mapper.model.new(hash)
# returns { id: 1, name: 'test', area: { id: 2, name: "area1"} }

```

But then I cannot get Bookings from repo, without Area loaded.

Thank you for help!  
PS: I don’t want to map to custom objects. I want to use `auto_struct` with custom struct classes.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [January 30, 2022, 4:01pm UTC](https://discourse.rom-rb.org/t/how-to-create-entity-programmatically-with-relations/498/2 "2022-01-30T16:01:48Z")

</div>

You need to do `bookings.combine(:area).mapper.model.new(hash)`. A booking without an area is not the same entity class as a booking **with** an area.

---

<div class="post-metadata">

**Author:** ![wuarmin](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/wuarmin/32/233_2.png) [@wuarmin](https://discourse.rom-rb.org/u/wuarmin)\
**Post date:** [February 3, 2022, 7:28am UTC](https://discourse.rom-rb.org/t/how-to-create-entity-programmatically-with-relations/498/3 "2022-02-03T07:28:12Z")

</div>

Thank you! The more insight I get into ROM, the more awesome I find the object mapper. Thanks for the great work. Long live ruby 😉
