# Problem with polymorphic

**URL:** <https://www.ruby-forum.com/t/problem-with-polymorphic/181568>\
**Category:** Rails\
**Created:** [January 13, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/problem-with-polymorphic/181568 "2010-01-13T22:53:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zumus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/67e7ee/32.png) [@zumus](https://www.ruby-forum.com/u/zumus)\
**Post date:** [January 13, 2010, 10:53pm UTC](https://www.ruby-forum.com/t/problem-with-polymorphic/181568/1 "2010-01-13T22:53:27Z")

</div>

Hi all! I have a strange problem when i try to use polymorphic  
associations in my app. I have a simple relations between my models:

class Item \< ActiveRecord::Base  
belongs\_to :content, :polymorphic =\> true, :dependent =\> :destroy

class Page \< ActiveRecord::Base  
has\_one :item, :as =\> :content, :dependent =\> :destroy

When i add a new Item everything works fine, all associations creates  
and all looks great, but when i do like this

@item = Item.find(1)  
@item.destroy

or even when i try to do this  
@page = Item.find(1)  
@page.destroy

I get “ActiveRecord::RecordNotFound: Couldn’t find Page without an ID”  
error.

Of course, page and item with id=1 are exist.

What i did wrong?

---

<div class="post-metadata">

**Author:** ![zumus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/67e7ee/32.png) [@zumus](https://www.ruby-forum.com/u/zumus)\
**Post date:** [January 13, 2010, 11:10pm UTC](https://www.ruby-forum.com/t/problem-with-polymorphic/181568/2 "2010-01-13T23:10:20Z")

</div>

On Wed, Jan 13, 2010 at 1:53 PM, Artem Bu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and all looks great, but when i do like this
> 
> Of course, page and item with id=1 are exist.
> 
> What i did wrong?

## Not sure if it’ll help, but I’ve found find\_by\_id more developer friendly than find. You could try it and see what results you get. I’m betting a NoMethodError on nil for delete.

# Curtis C. removed\_email\_address@domain.invalid home:[http://curtiscooley.com](http://curtiscooley.com) blog:[http://ponderingobjectorienteddesign.blogspot.com](http://ponderingobjectorienteddesign.blogspot.com)

Leadership is a potent combination of strategy and character. But if  
you must be without one, be without the strategy.  
– H. Norman Schwarzkopf

---

<div class="post-metadata">

**Author:** ![zumus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/67e7ee/32.png) [@zumus](https://www.ruby-forum.com/u/zumus)\
**Post date:** [January 13, 2010, 11:21pm UTC](https://www.ruby-forum.com/t/problem-with-polymorphic/181568/3 "2010-01-13T23:21:23Z")

</div>

Curtis C. wrote:

> On Wed, Jan 13, 2010 at 1:53 PM, Artem Bu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > and all looks great, but when i do like this
> > 
> > Of course, page and item with id=1 are exist.
> > 
> > What i did wrong?
> 
> Not sure if it’ll help, but I’ve found find\_by\_id more developer  
> friendly than find. You could try it and see what results you get. I’m  
> betting a NoMethodError on nil for delete.

Thanks for answer, but i didn’t help. @page or @item are found great by  
find find\_by\_id. But i think problem occurs then app trying to find and  
destroy page nested in item.

---

<div class="post-metadata">

**Author:** ![zumus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/67e7ee/32.png) [@zumus](https://www.ruby-forum.com/u/zumus)\
**Post date:** [January 14, 2010, 2:29pm UTC](https://www.ruby-forum.com/t/problem-with-polymorphic/181568/4 "2010-01-14T14:29:50Z")

</div>

On Jan 13, 4:53Â pm, Artem Bu [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and all looks great, but when i do like this

Can you post a complete stacktrace? It will be a lot easier to figure  
out what the issue is.

–Matt J.
