Hi,
I'm trying to setup a model relationship like this:
I have an Image model where I can associate it with any other model
(like Pages or Articles) using a polymorphic relationship with a join
table, so I can have more than one resource using the same image.
Here is my models:
class Image < ActiveRecord::Base
has_many :imageables
has_many :resources, :through => :imageables
end
class Imageable < ActiveRecord::Base
belongs_to :image
belongs_to :resource, :polymorphic => true
end
class Page < ActiveRecord::Base
has_many :imageables, :as => :resource, :dependent
=> :destroy, :include => :image
has_many :images, :through => :imageables
end
My ImagesController:
class ImagesController < ApplicationController
resource_controller
belongs_to :page
end
The problem is, when I access http://localhost:3000/pages/1/images/new
I get the following error:
Cannot associate new records through 'Page#imageables' on '#'. Both
records must have an id in order to create the has_many :through
record associating them.
Is there a way to make it work?
Thanks.