Hello out there, let''s assume, I''ve got an article model. An article always has an author and refers to it. But I''ve got several types of authors which all have theier own models. So a simple foreign key rather belongs_to will not help me. So, I could save the author_id and a kind of authot_type to manually create the association. Is there a more comfortable way in rails? Thank you very much! :) ms --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Read this: http://wiki.rubyonrails.org/rails/pages/polymorphicassociations - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Sun, Feb 8, 2009 at 3:55 PM, ms <ms-cGBD8117FJM@public.gmane.org> wrote:> > Hello out there, > > let''s assume, I''ve got an article model. An article always has an > author and refers to it. But I''ve got several types of authors which > all have theier own models. So a simple foreign key rather belongs_to > will not help me. So, I could save the author_id and a kind of > authot_type to manually create the association. Is there a more > comfortable way in rails? > > Thank you very much! :) > ms > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thank you for your answer. I read this article before, but I can''t map this technique to my problem. Maybe you can help me with this? Thank you! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
class Article < ActiveRecord::Base belongs_to :author, :polymorphic => true end And then to assign an author: article = Article.new article.author = some_author article.save - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Sun, Feb 8, 2009 at 4:06 PM, ms <ms-cGBD8117FJM@public.gmane.org> wrote:> > Thank you for your answer. I read this article before, but I can''t map > this technique to my problem. Maybe you can help me with this? > > Thank you! > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
BTW, your table should have an "author_id" column (usually an integer) and an "author_type" column (usually a varchar). - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Sun, Feb 8, 2009 at 4:09 PM, Maurício Linhares <mauricio.linhares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class Article < ActiveRecord::Base > belongs_to :author, :polymorphic => true > end > > And then to assign an author: > > article = Article.new > article.author = some_author > article.save > > - > Maurício Linhares > http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) > > > > On Sun, Feb 8, 2009 at 4:06 PM, ms <ms-cGBD8117FJM@public.gmane.org> wrote: >> >> Thank you for your answer. I read this article before, but I can''t map >> this technique to my problem. Maybe you can help me with this? >> >> Thank you! >> >> >> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> > class Article < ActiveRecord::Base > > belongs_to :author, :polymorphic => true > > end > > > And then to assign an author: > > > article = Article.new > > article.author = some_author > > article.saveBut I''ve got no no author model, since author is nothing concrete in my data model. Doesn''t this matter? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
No, the "author" can be any of you models that descend from ActiveRecord::Base. - Maurício Linhares http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en) On Sun, Feb 8, 2009 at 4:18 PM, ms <ms-cGBD8117FJM@public.gmane.org> wrote:> > But I''ve got no no author model, since author is nothing concrete in > my data model. Doesn''t this matter?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hey, fantastic thing and so easy, thank you! On 8 Feb., 20:19, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> No, the "author" can be any of you models that descend from ActiveRecord::Base. > > - > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > > On Sun, Feb 8, 2009 at 4:18 PM, ms <m...-cGBD8117FJM@public.gmane.org> wrote: > > > But I''ve got no no author model, since author is nothing concrete in > > my data model. Doesn''t this matter?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Polymorphic associations Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 09/02/2009, at 5:55 AM, ms <ms-cGBD8117FJM@public.gmane.org> wrote:> > Hello out there, > > let''s assume, I''ve got an article model. An article always has an > author and refers to it. But I''ve got several types of authors which > all have theier own models. So a simple foreign key rather belongs_to > will not help me. So, I could save the author_id and a kind of > authot_type to manually create the association. Is there a more > comfortable way in rails? > > Thank you very much! :) > ms > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Why not? Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 09/02/2009, at 6:06 AM, ms <ms-cGBD8117FJM@public.gmane.org> wrote:> > Thank you for your answer. I read this article before, but I can''t map > this technique to my problem. Maybe you can help me with this? > > Thank you! > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Um dude you''re talking single table inheritance not polymorphic associations which is what he''s after Blog: http://random8.zenunit.com/ Learn rails: http://sensei.zenunit.com/ On 09/02/2009, at 6:19 AM, Maurício Linhares <mauricio.linhares@gmail.co m> wrote:> > No, the "author" can be any of you models that descend from > ActiveRecord::Base. > > - > Maurício Linhares > http://alinhavado.wordpress.com/ (pt-br) | http:// > blog.codevader.com/ (en) > > On Sun, Feb 8, 2009 at 4:18 PM, ms <ms-cGBD8117FJM@public.gmane.org> wrote: >> >> But I''ve got no no author model, since author is nothing concrete in >> my data model. Doesn''t this matter? > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---