# Instantiating a custom Struct instance

**URL:** <https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224>\
**Category:** Usage Stories\
**Created:** [April 8, 2018, 8:38pm UTC](https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224 "2018-04-08T20:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)\
**Post date:** [April 8, 2018, 8:38pm UTC](https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224/1 "2018-04-08T20:38:57Z")

</div>

I have an `Events` relation that defines a schema. I’ve configured it to use the `Event` struct, which inherits from `ROM::Struct`. How can I instantiate a new `Event` instance and then use the `create` command to insert new records into my dataset? Do I have to define the attributes on the custom struct? What if I’m trying to take advantage of the `infer: true` option?

Thanks.

---

<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:** [April 9, 2018, 12:06pm UTC](https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224/2 "2018-04-09T12:06:26Z")

</div>

> How can I instantiate a new Event instance and then use the create command to insert new records into my dataset?

You can get the struct class via `relation.mapper.model`, but I would recommend to use plain hashes with commands/changesets.

> Do I have to define the attributes on the custom struct?

No, the whole point of auto-structs is to avoid having to define attributes manually.

> What if I’m trying to take advantage of the infer: true option?

`infer: true` is for relation schemas, are we talking about rom-sql here?

---

<div class="post-metadata">

**Author:** ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)\
**Post date:** [April 13, 2018, 12:23am UTC](https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224/3 "2018-04-13T00:23:34Z")

</div>

Hi @solnic,

Yeah, this is in the context of rom-sql. I was having trouble getting the `infer: true` to load the schema to then coerce data. However, I’ve worked around that issue by explicitly defining the schema in the relation. Furthermore, this is also in the context of getting a test suite working, in terms of getting this to all work together.

I’ve been creating a rom-sql adapter for the RailsEventStore project to use their Event Sourcing model with ROM. This has been my first use of ROM to learn the framework. I have many more questions, but this project will provide some context for how I’ve got things to work: [https://github.com/RailsEventStore/rails\_event\_store/tree/master/ruby\_event\_store\_rom\_sql](https://github.com/RailsEventStore/rails_event_store/tree/master/ruby_event_store_rom_sql)

I just recently was trying to figure out how to specify a default mapper on a repository or a relation. Is that possible? The goal is to use the mapper to map entities to a custom class, but it seems I have to call `map_with` in each repository method before returning a result?

Thanks for the help!

~Joel

---

<div class="post-metadata">

**Author:** ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)\
**Post date:** [April 13, 2018, 12:26am UTC](https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224/4 "2018-04-13T00:26:02Z")

</div>

@solnic I want to make one more note that my goal is to refactor the RailsEventStore ROM adapter to make the relation classes implement an interface that the repository classes use, so that the backing store can be interchangeable. I’m interested in your thoughts or direction on that, to allow ROM to be utilized as a way to make backing stores more pluggable.

Thanks.

---

<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:** [April 17, 2018, 12:22pm UTC](https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224/5 "2018-04-17T12:22:17Z")

</div>

> I’m interested in your thoughts or direction on that, to allow ROM to be utilized as a way to make backing stores more pluggable.

Yes you can use ROM to achieve that. All you need is a common relation abstraction that is implemented by all your relations, regardless of the backend. Then the repo doesn’t care which relations it uses, because the relation API is the same. You can then initialize this repo using different rom containers, where each container can provide different backends.

---

<div class="post-metadata">

**Author:** ![joelvh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/joelvh/32/139_2.png) [@joelvh](https://discourse.rom-rb.org/u/joelvh)\
**Post date:** [April 17, 2018, 6:36pm UTC](https://discourse.rom-rb.org/t/instantiating-a-custom-struct-instance/224/6 "2018-04-17T18:36:52Z")

</div>

@solnic yep! That’s the idea. There are some methods I’d assume might be built-in to the relation by default - are there any plans to expand the default methods, or is it not meant to provide a “standard” interface with broader capabilities?

Specifically, `limit` to limit the number of records returned or an “offset” as well seemed like a natural built-in… maybe even ability to specify a range like `relation[3..7]` to specify an offset.

Next, I’m going to be working with `Session` to use the transaction abstraction and see how that goes…
