Google Groups Home
Help | Sign in
ActiveRecord::HasManyThroughCa ntAssociateNewRecords (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.)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Lucas  
View profile
 More options Apr 21, 12:08 am
From: Lucas <lucas.mun...@gmail.com>
Date: Sun, 20 Apr 2008 21:08:35 -0700 (PDT)
Local: Mon, Apr 21 2008 12:08 am
Subject: ActiveRecord::HasManyThroughCantAssociat eNewRecords (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.)
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.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bartek  
View profile
 More options Apr 23, 6:34 pm
From: Bartek <grinkiew...@gmail.com>
Date: Wed, 23 Apr 2008 15:34:43 -0700 (PDT)
Local: Wed, Apr 23 2008 6:34 pm
Subject: Re: ActiveRecord::HasManyThroughCantAssociat eNewRecords (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.)
Try adding the following code to your  ImagesController and see if it
helps:

  create.after do
    @page.images << @image if parent_type == :page
  end

  private

  def build_object
    @object ||= Image.new
  end

Bartek


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bartek  
View profile
 More options Apr 23, 6:41 pm
From: Bartek <grinkiew...@gmail.com>
Date: Wed, 23 Apr 2008 15:41:25 -0700 (PDT)
Local: Wed, Apr 23 2008 6:41 pm
Subject: Re: ActiveRecord::HasManyThroughCantAssociat eNewRecords (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.)
I forgot about "object_params", here is the correction:

  create.after do
    @page.images << @image if parent_type == :page
  end

  private

  def build_object
    @object ||= Image.new object_params
  end


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sean Schofield  
View profile
 More options May 14, 5:10 pm
From: Sean Schofield <sean.schofi...@gmail.com>
Date: Wed, 14 May 2008 14:10:26 -0700 (PDT)
Local: Wed, May 14 2008 5:10 pm
Subject: Re: ActiveRecord::HasManyThroughCantAssociat eNewRecords (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.)
FYI.  I was running into the same problem using has_many_polymorphs
and this solution also worked for me as well.

Sean

On Apr 23, 6:41 pm, Bartek <grinkiew...@gmail.com> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google