# Problem when editing record in polymorphic relation

**URL:** <https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777>\
**Category:** Rails\
**Created:** [January 2, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777 "2008-01-02T15:22:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [January 2, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777/1 "2008-01-02T15:22:38Z")

</div>

# I have the following three models created applying the polymorphic concept

class SoftwareCi \< ActiveRecord::Base  
has\_one :ci, :as =\> :content  
end

class HardwareCi \< ActiveRecord::Base  
has\_one :ci, :as =\> :content  
end

class Ci \< ActiveRecord::Base  
belongs\_to :content, :polymorphic =\> true  
end

# The table, cis ,contains the following records

id | ci\_number | content\_id | content\_type  
----±---------±----------±-----------±-------------  
1 | CI1 | 1 | SoftwareCi  
2 | CI2 | 2 | SoftwareCi  
3 | CI3 | 1 | HardwareCi  
4 | CI4 | 2 | HardwareCi

# The table, software\_cis ,contains the following records

id | asset\_tag | status | version  
----±------------------±--------±--------  
1 | AC Logix - AT1 | Status2 | 1.0  
2 | RR Logix AT2 | Status3 | 2.3

# The table, hardware\_cis ,contains the following records

id | asset\_tag | status | version  
----±------------------±--------±--------  
1 | HCL - AT1 | Status1 | 1.0  
2 | IBM - AT2 | Status1 | 2.3

# The controller, ci\_controller, contains the edit method as follows

def edit  
@ci=Ci.find(1)  
end

# My Problem

In the edit.rhtml file, I am trying to populate the value of ‘version’,  
an attribute in the software\_ci table which is represented by the  
content\_type, SotwareCi. The sample code is given below.

\<%= text\_field ci.content, “version”, “size” =\> 20 %\>

I am assuming ‘ci.content’ in the above code will represent the model  
and ‘version’ will represent the attribute of that model.

When executed the above code didn’t work.  
What is wrong here? How I have to proceed?  
If we want to simply display the value of ci.content.version, it works  
but when tried to edit, it doesn’t work

Thanks in advance for your help

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [January 2, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777/2 "2008-01-02T15:37:06Z")

</div>

On 2 Jan 2008, at 14:22, Venu Vallayil wrote:

> # My Problem
> 
> In the edit.rhtml file, I am trying to populate the value of  
> ‘version’,  
> an attribute in the software\_ci table which is represented by the  
> content\_type, SotwareCi. The sample code is given below.
> 
> \<%= text\_field ci.content, “version”, “size” =\> 20 %\>

text\_field doesn’t work that way - it expects the first parameter to  
be the name of an instance variable  
You should look into form\_for since it allows you to escape that  
convention

Fred

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [January 2, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777/3 "2008-01-02T15:57:08Z")

</div>

Frederick C. wrote:

> On 2 Jan 2008, at 14:22, Venu Vallayil wrote:
> 
> > # My Problem
> > 
> > In the edit.rhtml file, I am trying to populate the value of  
> > ‘version’,  
> > an attribute in the software\_ci table which is represented by the  
> > content\_type, SotwareCi. The sample code is given below.
> > 
> > \<%= text\_field ci.content, “version”, “size” =\> 20 %\>
> 
> text\_field doesn’t work that way - it expects the first parameter to  
> be the name of an instance variable  
> You should look into form\_for since it allows you to escape that  
> convention
> 
> Fred

I created as follows but didn’t work

\<% form\_for :ci, @ci, :url =\> { :action =\> “update” } do |f| %\>  
CI Name: \<%= f.content.text\_field :name %\>  
\<% end %\>

Thanks  
Venu

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [January 2, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777/4 "2008-01-02T16:20:32Z")

</div>

On 2 Jan 2008, at 14:57, Venu Vallayil wrote:

> > >
> 
> \<% form\_for :ci, @ci, :url =\> { :action =\> “update” } do |f| %\>  
> CI Name: \<%= f.content.text\_field :name %\>  
> \<% end %\>

That doesn’t mean anything - you need  
form\_for :co, @ci.content, … do |f|  
f.text\_field :name  
end

If the form as a whole should apply to ci rather than its content then  
you can use fields\_for to create the fields (without creating the

 tag)

Fred

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [January 2, 2008, 4:44pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777/5 "2008-01-02T16:44:01Z")

</div>

On 2 Jan 2008, at 15:34, Venu Vallayil wrote:

> > That doesn’t mean anything - you need
> 
> Fred…will you be able to show me a sample code that tells how a  
> record is to be edited when there exists a polymorphic relation as  
> shown  
> in the starting thread of this talk

The polymorphism is (I think) irrelevant, it’s the fact that there is  
an association. There is a good series on [railscasts.com](http://railscasts.com) about complex  
forms.

Fred

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [January 2, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777/6 "2008-01-02T16:52:46Z")

</div>

Frederick C. wrote:

> On 2 Jan 2008, at 15:34, Venu Vallayil wrote:
> 
> > > That doesn’t mean anything - you need
> > 
> > Fred…will you be able to show me a sample code that tells how a  
> > record is to be edited when there exists a polymorphic relation as  
> > shown  
> > in the starting thread of this talk
> 
> The polymorphism is (I think) irrelevant, it’s the fact that there is  
> an association. There is a good series on [railscasts.com](http://railscasts.com) about complex  
> forms.
> 
> Fred

But nobody talks/shows the code about editing a record keeping the  
concept I introduced in my starting thread…seems to be i am lost  
somewhere…can’t proceed…thanks for your support. Really  
appreciate your support.

---

<div class="post-metadata">

**Author:** ![venu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/9de0a6/32.png) [@venu](https://www.ruby-forum.com/u/venu)\
**Post date:** [January 2, 2008, 4:34pm UTC](https://www.ruby-forum.com/t/problem-when-editing-record-in-polymorphic-relation/124777/7 "2008-01-02T16:34:55Z")

</div>

Frederick C. wrote:

> On 2 Jan 2008, at 14:57, Venu Vallayil wrote:
> 
> > > >
> > 
> > \<% form\_for :ci, @ci, :url =\> { :action =\> “update” } do |f| %\>  
> > CI Name: \<%= f.content.text\_field :name %\>  
> > \<% end %\>
> 
> That doesn’t mean anything - you need  
> form\_for :co, @ci.content, … do |f|  
> f.text\_field :name  
> end
> 
> If the form as a whole should apply to ci rather than its content then  
> you can use fields\_for to create the fields (without creating the
> 
> tag)
> 
> Fred

Fred…will you be able to show me a sample code that tells how a  
record is to be edited when there exists a polymorphic relation as shown  
in the starting thread of this talk

Thanks  
Venu
