Polymorphic relationships?

I´ll make a relationship between "notificaciones" and "users" as "origen" and "destinatarios" respectively.At the moment i´m working with the "origen" relationship with bad results...Rails accepts the Notificacion.origen = User asignement, but don´t stores nothing in the database.

You can see..

#I get the Notificacion with no origen

n =Notificacion.last

=> #<Notificacion id: 1, evento_id: 1, origen: nil, correo: nil, created_at: "2009-10-20 07:44:53", updated_at: "2009-10-20 08:17:10">

#I review that no origen is set

n.origen

=> nil

#le asigno el primer usuario como origen, parece que le gusta #I make the relation...seems that Rails like that..

n.origen = User.first

=> #<User id: 5, login: "ernesto", email: "ernesto.suarez AT mydomain", crypted_password: "64834e29e801af32958a2f6be5852f2b78bbe", salt: "8f2c260522af24da5472fef1eec29e7cb6af1", created_at: "2009-06-02 16:19:04", updated_at: "2009-09-24 07:34:19", remember_token: "269b74d1b30efea0cdfd3007916d6c5ffe17", remember_token_expires_at: "2009-10-08 07:34:19", activation_code: nil, activated_at: "2009-06-02 16:20:26", state: "active", deleted_at: nil, photo_file_name: "hax.jpg", photo_content_type: "image/jpeg", photo_file_size: 35458, nombre: "Jose Ernesto", trafico_id: 1>

#consulto el atributo y me devuelve un User...parece que todo marcha #I query the attribute and get the spected response

n.origen

=> #<User id: 5, login: "ernesto", email: "ernesto.suarez AT mydomain", crypted_password: "64834e29e801af32958a2f6be5852f2b78bbe", salt: "8f2c260522af24da5472fef1eec29e7cb6af1", created_at: "2009-06-02 16:19:04", updated_at: "2009-09-24 07:34:19", remember_token: "269b74d1b30efea0cdfd3007916d6c5ffe17", remember_token_expires_at: "2009-10-08 07:34:19", activation_code: nil, activated_at: "2009-06-02 16:20:26", state: "active", deleted_at: nil, photo_file_name: "hax.jpg", photo_content_type: "image/jpeg", photo_file_size: 35458, nombre: "Jose Ernesto", trafico_id: 1>

#I query the register but...surprise! Origen is null!

n

=> #<Notificacion id: 1, evento_id: 1, origen: nil, correo: nil, created_at: "2009-10-20 07:44:53", updated_at: "2009-10-20 08:17:10">

#Save the register, seems that Rails like that too...

n.save

=> true

#but i get the same result...

n.origen

=> #<User id: 5, login: "ernesto", email: "ernesto.suarez AT mydomain", crypted_password: "64834e29e801af32958a2f6be5852f2b78bbe", salt: "8f2c260522af24da5472fef1eec29e7cb6af1", created_at: "2009-06-02 16:19:04", updated_at: "2009-09-24 07:34:19", remember_token: "269b74d1b30efea0cdfd3007916d6c5ffe17", remember_token_expires_at: "2009-10-08 07:34:19", activation_code: nil, activated_at: "2009-06-02 16:20:26", state: "active", deleted_at: nil, photo_file_name: "hax.jpg", photo_content_type: "image/jpeg", photo_file_size: 35458, nombre: "Jose Ernesto", trafico_id: 1>

n

=> #<Notificacion id: 1, evento_id: 1, origen: nil, correo: nil, created_at: "2009-10-20 07:44:53", updated_at: "2009-10-20 09:37:09">

I reading about polymorphic relationships but i think that this isn´t necesary.. or maybe?Rails says: "ArgumentError: Unknown key(s): polymorphic" when I try to use them.

Any suggestions?

These are my relationships:

class Notificacion < ActiveRecord::Base   belongs_to :evento   belongs_to :origen, :class_name => 'User'   has_many :destinatarios,:through => :destinatarios,:source => 'User' ....

class User < ActiveRecord::Base   has_many :notificaciones,:class_name => 'Notificacion',:as => 'origen'   has_many :notificaciones,:as => 'destinatario', :through => :destinatarios ....

You've got a field named wrong here; if you want Notificacion to belongs_to User, you'll need to name the foreign key origen_id; I'd guess that the various attribute setters are running headlong into each other causing the behavior you've described.

--Matt Jones

Thanks Matt.. it´s exactly that was happened! Now it runs correctly.