Problem with polymorphic

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?

On Wed, Jan 13, 2010 at 1:53 PM, Artem Bu [email protected] 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.
[email protected]
home:http://curtiscooley.com
blog: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

Curtis C. wrote:

On Wed, Jan 13, 2010 at 1:53 PM, Artem Bu [email protected] 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.

On Jan 13, 4:53Â pm, Artem Bu [email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs