# Replace ActiveModel in form\_for, form\_with with dry family

**URL:** <https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084>\
**Category:** dry-validation\
**Created:** [July 3, 2020, 10:44am UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084 "2020-07-03T10:44:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![woto](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/woto/32/529_2.png) [@woto](https://discourse.dry-rb.org/u/woto)\
**Post date:** [July 3, 2020, 10:44am UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/1 "2020-07-03T10:44:26Z")

</div>

Hi to all. Sorry for short question, it may seem frivolous to you. But i’ve really googled it and didn’t find any confident response or example.

Can I replace ActiveModel with dry-schema, dry-struct, dry-validation in rails constructions with form\_for, form\_with? Does they suggest such use? Are there any pitfalls?

Anyway I’ll try to dig deeper while waiting an answer 🙂  
Thank you

---

<div class="post-metadata">

**Author:** ![woto](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/woto/32/529_2.png) [@woto](https://discourse.dry-rb.org/u/woto)\
**Post date:** [July 6, 2020, 10:55pm UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/2 "2020-07-06T22:55:41Z")

</div>

I’ve made it with ActiveModel. But anyway I appreciate your answer if it ever will be given :slight\_smile 🙂

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [July 10, 2020, 4:56am UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/3 "2020-07-10T04:56:42Z")

</div>

Sure, it’s certainly possible to do this. I’ve done it plenty of times with plain Ruby classes, so dry-struct would just be a shortcut for attribute definition and type-checking.

It’s important to understand the conceptual differences between ActiveModel and Dry-Struct. The Dry system is intended to work in a functional way, with immutable structs. The sort of mutation you typically do with activemodel objects is not supported, by design.

That means validation is an entirely separate step that takes place before instantiating the struct objects. You’ll need to stitch the two processes together in some way, which is easy with dry-monads. See also dry-rails for how you would integrate dry-schema into a Rails controller.

If you’re unfamiliar with _all_ of this, I would recommend taking a piece at a time and solving a single problem with it. It will become clear over time how they work together, but in my experience trying to use it all at once is too hard.

The minimum-viable form object would look something like this:

```ruby
module T
  include Dry::Types(default: :strict)

  Username = Strict::String.constrained(min_size: 3)
end

class Form < Dry::Struct
  extend ActiveModel::Naming
  include ActiveModel::AttributeMethods

  attribute :username, T::Username

  def persisted?
    false
  end
end

```

---

<div class="post-metadata">

**Author:** ![woto](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/woto/32/529_2.png) [@woto](https://discourse.dry-rb.org/u/woto)\
**Post date:** [September 26, 2020, 2:03am UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/4 "2020-09-26T02:03:52Z")

</div>

Oh, damn missed your post and found my question in google 🙂 will try your advice. Thank you )

---

<div class="post-metadata">

**Author:** ![tmaier](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/tmaier/32/772_2.png) [@tmaier](https://discourse.dry-rb.org/u/tmaier)\
**Post date:** [June 19, 2022, 7:58am UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/5 "2022-06-19T07:58:16Z")

</div>

I am bugged by the same question and wonder of the lack of documentation or blog posts in this regard, as I would expect this to be a quite common problem.

@woto, how did you solve it in the end? Was this enough or are there any learnings to share from your side?

---

<div class="post-metadata">

**Author:** ![woto](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/woto/32/529_2.png) [@woto](https://discourse.dry-rb.org/u/woto)\
**Post date:** [July 6, 2022, 10:54pm UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/6 "2022-07-06T22:54:01Z")

</div>

Nope, it seems they are not interchangeable. Only hard way. With additional layer of code. For example as @alassek mentioned.

---

<div class="post-metadata">

**Author:** ![jedschneider](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/jedschneider/32/617_2.png) [@jedschneider](https://discourse.dry-rb.org/u/jedschneider)\
**Post date:** [July 11, 2022, 2:45pm UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/7 "2022-07-11T14:45:44Z")

</div>

rom-rb has the notion of changesets, which are designed for the use case of validation, I wonder if something like Hanami has a reasonable alternative to `form-for` that integrates more natively with changesets.

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [July 13, 2022, 4:27am UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/8 "2022-07-13T04:27:39Z")

</div>

I think you’re misunderstanding the use-case of changesets, I don’t think they have anything to do with validation.

> They work on top of commands, and provide additional data mapping functionality and have support for associating data.

> **[ROM - Introduction](https://rom-rb.org/learn/changeset/5.2/)**
>
> An open-source persistence and mapping toolkit for Ruby built for speed and simplicity.

---

<div class="post-metadata">

**Author:** ![jedschneider](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/jedschneider/32/617_2.png) [@jedschneider](https://discourse.dry-rb.org/u/jedschneider)\
**Post date:** [July 13, 2022, 5:03pm UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/9 "2022-07-13T17:03:12Z")

</div>

My understanding is that a changset is a data structure that assists in the update of immutable data objects (like structs) by providing a mapping between new and old values. At least in the case of Ecto, invalid updates and their reason for being invalid are included in the changeset.

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [July 14, 2022, 5:49pm UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/10 "2022-07-14T17:49:43Z")

</div>

The implementation of Changeset is just combining the output of Transproc with a Command, so it deals with transformation and persistence. Validation would happen before the Changeset, using dry-validation.

Perhaps you could integrate a validation contract in a Changeset, but it is not obvious to me how you would do that, because Changeset doesn’t use Result types consistently.

---

<div class="post-metadata">

**Author:** ![jedschneider](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/jedschneider/32/617_2.png) [@jedschneider](https://discourse.dry-rb.org/u/jedschneider)\
**Post date:** [July 14, 2022, 11:44pm UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/11 "2022-07-14T23:44:19Z")

</div>

its seems its not an abstraction that has much meaning between data mapper implementations. [Ecto.Changeset — Ecto v3.8.4](https://hexdocs.pm/ecto/Ecto.Changeset.html)

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [July 15, 2022, 2:15am UTC](https://discourse.dry-rb.org/t/replace-activemodel-in-form-for-form-with-with-dry-family/1084/12 "2022-07-15T02:15:23Z")

</div>

I think it is fair to say that `ROM::ChangeSet` is not yet a fully realized idea, and it would be nice for it to work more like `Ecto.ChangeSet`.

You can still achieve the same things, it just takes a little more effort right now.
