Topic: Polymorphic associations don't get saved

Hi,

I try to implement a polymorphic association. Everything seems to work, besides that the associated object dont't get saved.

Here's my simple code:

Simple Person Class

class Person < ActiveRecord::Base
  has_many :address_links, :as => :addressable, :dependent => :destroy
  has_many :addresses, :through => :address_links
end

The Linkmodel
class AddressLink < ActiveRecord::Base
  belongs_to :address
  belongs_to :addressable, :polymorphic => true
end

the polymorphic class
class Address < ActiveRecord::Base
  belongs_to :address_type
  has_many :address_links
end

I submit a new person and an address to the 'create' action in my person_controller:

  def create
    @person = Person.new(params[:person])
    @address = Address.new(params[:address])
    @person.addresses << @address
    @ok = @person.save ? true : false
  end

Everything seems alright. I checked the view, I checked the database. @address contains the attributes I submitted via my form, even @person.addresses contains the correct address. @person.save returns true. The adresse just never appears in the database.

Where's my mistake?

red

Re: Polymorphic associations don't get saved

Try saving the @address explicitly.

@ok = @person.save && @address.save

Railscasts - Free Ruby on Rails Screencasts

Re: Polymorphic associations don't get saved

Already tried that. The Address get's saved. Also the person. But not the link between person and address sad

red

Re: Polymorphic associations don't get saved

The "<<" adding syntax for a has_many :through association was recently added. Are you using the latest version of edge rails/1.2? Perhaps it is a bug. You can try saving the person and address before creating the link. Either that or create the link manually (AddressLink.new) and save that.

Railscasts - Free Ruby on Rails Screencasts

Re: Polymorphic associations don't get saved

hum... I'm using plain rails from debian/unstable => 1.1.6 Seems I should use rails edge...

red

Re: Polymorphic associations don't get saved

Weird, I didn't think the << syntax was supported in a has_many :through association in 1.1.6. I might be wrong.

Edit: Perhaps it's not supported, and simply doesn't report an error, because what it ends up being is appending the address to the addresses array. It doesn't actually build the association/link.

A solution is to build the link manually (unless you want to upgrade to 1.2/edge which I believe supports the <<.

@person = Person.new(params[:person])
@address = Address.new(params[:address])
@person.address_links.build(:address => @address)
if @address.save && @person.save
  #..
end

Last edited by ryanb (2007-01-10 15:55:03)

Railscasts - Free Ruby on Rails Screencasts

Re: Polymorphic associations don't get saved

ryanb wrote:

Weird, I didn't think the << syntax was supported in a has_many :through association in 1.1.6. I might be wrong.

Yes, it's not supported in 1.1.6. I just changed my has_many :through assiciation to a simple polymorphic one. But I have another problem with rails 1.1.6, so I'm going to install Rails edge and maybe i'll check the has_many :through later...

thanks for your time.
red

Re: Polymorphic associations don't get saved

I made an edit above (guess I wasn't fast enough wink), try that.

Railscasts - Free Ruby on Rails Screencasts