# Polymorphic relationships?

**URL:** <https://www.ruby-forum.com/t/polymorphic-relationships/177569>\
**Category:** Rails\
**Created:** [October 20, 2009, 12:03pm UTC](https://www.ruby-forum.com/t/polymorphic-relationships/177569 "2009-10-20T12:03:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [October 20, 2009, 12:03pm UTC](https://www.ruby-forum.com/t/polymorphic-relationships/177569/1 "2009-10-20T12:03:33Z")

</div>

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  
…

---

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [October 21, 2009, 3:25am UTC](https://www.ruby-forum.com/t/polymorphic-relationships/177569/2 "2009-10-21T03:25:44Z")

</div>

On Oct 20, 6:02 am, Jose Ernesto S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> =\> #\<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”\>

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 J.

---

<div class="post-metadata">

**Author:** ![jsierra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c37758/32.png) [@jsierra](https://www.ruby-forum.com/u/jsierra)\
**Post date:** [October 21, 2009, 9:59am UTC](https://www.ruby-forum.com/t/polymorphic-relationships/177569/3 "2009-10-21T09:59:09Z")

</div>

On 21 oct, 03:24, Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –Matt J.  
> Thanks Matt… it´s exactly that was happened! Now it runs correctly.
