# How do I change :type attribute in a view?

**URL:** <https://www.ruby-forum.com/t/how-do-i-change-type-attribute-in-a-view/99032>\
**Category:** Rails\
**Created:** [July 19, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/how-do-i-change-type-attribute-in-a-view/99032 "2006-07-19T22:20:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [July 19, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/how-do-i-change-type-attribute-in-a-view/99032/1 "2006-07-19T22:20:01Z")

</div>

I’m using single table inheritance and want to change the :type  
attribute from an admin view. How do I do this given that :type has to  
be handled with special care?

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [July 20, 2006, 3:40am UTC](https://www.ruby-forum.com/t/how-do-i-change-type-attribute-in-a-view/99032/2 "2006-07-20T03:40:15Z")

</div>

Jose Hales-garcia wrote:

> I’m using single table inheritance and want to change the :type  
> attribute from an admin view. How do I do this given that :type has to  
> be handled with special care?

type is protected from mass assignment so you have to do something like:

@model = Model.find(params[:id])  
@model[:type] = params[:model][:type]  
@model.update\_attributes(params[:model])

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [July 21, 2006, 5:22am UTC](https://www.ruby-forum.com/t/how-do-i-change-type-attribute-in-a-view/99032/3 "2006-07-21T05:22:12Z")

</div>

Mark Reginald J. wrote:

> Jose Hales-garcia wrote:
> 
> > I’m using single table inheritance and want to change the :type  
> > attribute from an admin view. How do I do this given that :type has to  
> > be handled with special care?
> 
> type is protected from mass assignment so you have to do something like:
> 
> @model = Model.find(params[:id])  
> @model[:type] = params[:model][:type]  
> @model.update\_attributes(params[:model])

Thank you on the advice for the controller code. But I’m getting an  
error in the view (see below). Are there special techniques for  
handling the :type attribute inside of a view?

wrong argument type String (expected Module)  
Extracted source (around line #12):

9:

  
10:   
11:   
12: \<%= select :document, :type,  
ApplicationHelper::DOCUMENT\_TYPES %\>  
13:   
14:   
15:

---

<div class="post-metadata">

**Author:** ![jose](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@jose](https://www.ruby-forum.com/u/jose)\
**Post date:** [July 21, 2006, 7:58am UTC](https://www.ruby-forum.com/t/how-do-i-change-type-attribute-in-a-view/99032/4 "2006-07-21T07:58:54Z")

</div>

Jose Hales-Garcia wrote:

> Thank you on the advice for the controller code. But I’m getting an  
> error in the view (see below). Are there special techniques for  
> handling the :type attribute inside of a view?
> 
> wrong argument type String (expected Module)  
> Extracted source (around line #12):
> 
> 12:
> 
> \<%= select :document, :type, ApplicationHelper::DOCUMENT\_TYPES %\>

Yes, it’ll be using the Ruby built-in type method rather than the  
AR type attribute method because the latter uses method\_missing.

You’ll have to do one of three things:

1. Change to a different attribute name using set\_inheritance\_column
2. Add a method to the document model so that type can be accessed under  
a different method name
3. Code as:  
select\_tag ‘document[type]’,  
options\_for\_select(ApplicationHelper::DOCUMENT\_TYPES,  
@document[:type])

–  
We develop, watch us RoR, in numbers too big to ignore.
