# Dynamic attributes on a model

**URL:** <https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306>\
**Category:** Rails\
**Created:** [November 17, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306 "2006-11-17T14:34:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![isbof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@isbof](https://www.ruby-forum.com/u/isbof)\
**Post date:** [November 17, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306/1 "2006-11-17T14:34:05Z")

</div>

We are in the middle of developing an application where we need to be  
able to add and remove attributes to a model when it is running. We  
have no problems removing the attribute with:

ActiveRecord::Schema.define do  
remove\_column [table\_name], [new\_colum\_name]  
end

but session that are already have loaded the object fails on update,  
because the attribute does not exits in the database.

Any one have any good idea on how to update the object already in  
session with the new schema information?

A side note: I know it is normal to move information like this out into  
its own table/model object, but because of performance issues we would  
like to keep all information in one table.

IsBOF

---

<div class="post-metadata">

**Author:** ![isbof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@isbof](https://www.ruby-forum.com/u/isbof)\
**Post date:** [November 17, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306/2 "2006-11-17T14:43:31Z")

</div>

Do you have to stick the whole object in the session or could you just  
store its id instead?

Fred

---

<div class="post-metadata">

**Author:** ![isbof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@isbof](https://www.ruby-forum.com/u/isbof)\
**Post date:** [November 17, 2006, 4:38pm UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306/3 "2006-11-17T16:38:30Z")

</div>

> Any one have any good idea on how to update the object already in  
> session with the new schema information?

Don’t know your exact scenario (have data you want to preserve in the  
object, etc…) but maybe class.reset\_column\_information with an  
object.reload … or something like that?

–  
Andrew S.

---

<div class="post-metadata">

**Author:** ![isbof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@isbof](https://www.ruby-forum.com/u/isbof)\
**Post date:** [November 17, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306/4 "2006-11-17T19:39:08Z")

</div>

Okay,

don’t think I’m picking on you or anything but it’s _really_  
frustrating to see this sort of thing.

Either your schema changes happen infrequently enough that a migrate/  
deploy is a reasonable strategy _or_ you are seriously abusing the  
relational model. I rarely feel issues have no middle-ground but in  
this instance… it’s one or the other.

Now, maybe you’ve _really_ done your homework and _proved_ (in a way  
to ensure Zed S. wouldn’t tear you a new one) that the _only_ way  
to get the performance you require is to batter your schema. But I  
suspect that’s not the case and you simply haven’t figured out the  
best way to solve your problem.

I’m available for mentoring and code reviews and my rates are  
reasonable so feel free to contact me off-list.

Of course, I’ve probably offended you so I’m not expecting an email -  
so find someone, _anyone_ good, to review your code show you what you  
need to be doing in your particular case.

Good Luck  
Trevor

---

<div class="post-metadata">

**Author:** ![isbof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@isbof](https://www.ruby-forum.com/u/isbof)\
**Post date:** [November 19, 2006, 6:21am UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306/5 "2006-11-19T06:21:51Z")

</div>

IsBOF wrote:

> thanks for the suggestions.
> 
> 1. We use only the id in the session, it looks like rails caches some  
> information so it we get the “old” object schema with the field we have  
> removed back.
> 
> 2. reset\_column\_information - really resets the object information to  
> the point where it is unable to find any attributes or has\_many on the  
> object.
> 
> Trevor: We have tried with a “correct” relations model. This result in  
> a unacceptable performance issues (some thing like 200 updates on a  
> submit). And the remove/add of column happens frequently.
> 
> Best regards,
> 
> Rasmus

Rails was really not designed to handle this. In production mode, it  
caches the classes, which include schema info for Models. You could  
turn off cache class caching for production but that would create a huge  
performance drop.

---

<div class="post-metadata">

**Author:** ![isbof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@isbof](https://www.ruby-forum.com/u/isbof)\
**Post date:** [November 18, 2006, 9:56am UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306/6 "2006-11-18T09:56:20Z")

</div>

thanks for the suggestions.

1. We use only the id in the session, it looks like rails caches some  
information so it we get the “old” object schema with the field we have  
removed back.

2. reset\_column\_information - really resets the object information to  
the point where it is unable to find any attributes or has\_many on the  
object.

Trevor: We have tried with a “correct” relations model. This result in  
a unacceptable performance issues (some thing like 200 updates on a  
submit). And the remove/add of column happens frequently.

Best regards,

Rasmus

---

<div class="post-metadata">

**Author:** ![isbof](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@isbof](https://www.ruby-forum.com/u/isbof)\
**Post date:** [November 19, 2006, 6:42am UTC](https://www.ruby-forum.com/t/dynamic-attributes-on-a-model/76306/7 "2006-11-19T06:42:10Z")

</div>

On 11/19/06, Alex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > the point where it is unable to find any attributes or has\_many on the  
> > Rails was really not designed to handle this. In production mode, it  
> > caches the classes, which include schema info for Models. You could  
> > turn off cache class caching for production but that would create a huge  
> > performance drop.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Haha, yep, something doesn’t sound right… 🙂

Rasmus - maybe if you described the use case scenario a bit, we might be  
able to figure out a better strategy than adding/removing columns at run  
time. I can’t imagine ever wanting to do this.

–  
Scott B.  
Electro Interactive, Inc.  
Office: 813-333-5508  
[http://www.ElectroInteractive.com](http://www.ElectroInteractive.com) [http://www.electrointeractive.com/](http://www.electrointeractive.com/)
