# Inconsistent behaviour in plugins between development and production

**URL:** https://meta.discourse.org/t/inconsistent-behaviour-in-plugins-between-development-and-production/87220
**Category:** Development
**Created:** [May 10, 2018, 11:46pm UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-plugins-between-development-and-production/87220 "2018-05-10T23:46:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [May 10, 2018, 11:46pm UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-plugins-between-development-and-production/87220/1 "2018-05-10T23:46:10Z")

</div>

There is an inconsistency on how `add_to_serializer` works between development and production. This is due how plugins are applied

> <https://github.com/discourse/discourse/blob/main/lib/plugin/instance.rb#L600-L610>

Given we reload the code in development, every modification we do to a parent serializer, children will pick them up. To reproduce this inconsistency you can execute the code below

```ruby
add_to_serializer(:basic_user, :created_at) { user.created_at }

```

If you execute this in development, all descendants of `BasicUserSerializer` will have the `created_at` attribute. In production only `BasicUserSerializer` will have that attribute.

```ruby
UserNameSerializer._attributes
=> {:id=>:id, :username=>:username, :avatar_template=>:avatar_template, :name=>:name, :title=>:title}
BasicUserSerializer._attributes
=> {:id=>:id, :username=>:username, :avatar_template=>:avatar_template, :created_at=>:created_at}

```

The problem is due how `ActiveModel::Serializer` works, and I’m not sure if they want to support this use case, they only get the attributes from the parent once when you call the `attributes(*attrs)` method.

> <https://github.com/rails-api/active_model_serializers/blob/0-8-stable/lib/active_model/serializer.rb#L81>

## Workaround

Here’s a workaround to this problem, this is how I’m dealing with it atm.

Inside a plugin do

```ruby
after_initialize do
  # Monkey patch ActiveModel::Serializer to allow us
  # reload children serializers attributes after parent is modified
  class ::ActiveModel::Serializer
    # Update _attributes with superclass _attributes
    def self.reload
      self._attributes = _attributes.merge(superclass._attributes)
    end
  end

  add_to_serializer(:basic_user, :created_at) { user.created_at }

  # Reload all children serializers, so they include :created_at
  BasicUserSerializer.descendants.each(&:reload)
end

```

I saw some PRs from you @sam in the AMS repo, maybe you can take a look at this.

Thanks!

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [May 11, 2018, 4:14am UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-plugins-between-development-and-production/87220/2 "2018-05-11T04:14:08Z")

</div>

FYI there is an similar topic here [Plugin working locally But not in live - #4 by vinothkannans](https://meta.discourse.org/t/plugin-working-locally-but-not-in-live/60316/4). Anyway good catch and your workaround looks awesome.

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [May 11, 2018, 1:06pm UTC](https://meta.discourse.org/t/inconsistent-behaviour-in-plugins-between-development-and-production/87220/3 "2018-05-11T13:06:50Z")

</div>

I want to get this fixed in core, the behaviour should be the same between environments, but not sure how to approach it. I opened an issue in the AMS repo to get feedback there too [Runtime modification of parent serializer attributes doesn't update child attributes · Issue #2254 · rails-api/active\_model\_serializers · GitHub](https://github.com/rails-api/active_model_serializers/issues/2254)

Any suggestions?
