Marcos Korody
2012-Feb-14 09:54 UTC
A somewhat odd approach to an Active Record Association.
Hello there. Here''s my situation which I''d be thankfull to get a response that gets me on the right track to a solution: I have two different models. USERS and POSTS. Untill the present moment I have a pretty straight forward relationship between them. USER: has_many :posts, :dependent => :destroy POST: belongs_to :user This is working fine, but I want to add a different behaviour to it. I want a USER to also have the ability to set an original AUTHOR to the content of his POST. In other words, a POST belongs to a USER (submitter who''s posting some content) and also belongs to an original AUTHOR(a person who''s post content is originaly from). A new model AUTHOR would solve this problem easily. But the issue is that the AUTHORS table would have the same exact data as the USERS table. This is because all of my USERS, are also considered AUTHORS. And that being said, I shouldn''t really have a new model called AUTHORS cose it would be a duplicate. I need a way to refer to the USERS table as AUTHORS as well. So USER 1 is also an AUTHOR 1. All living in the same table. Thought about polymorphic associations but after a whole day of researches I am not convniced that this is the correct aproach to it. And if it is, how would that be done. I thank you in advance for any considerations on this subject. Please let me know if there is any other code you need to visualize in order to come up with a solution path. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2X1gqJT7wMkJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2012-Feb-14 10:05 UTC
Re: A somewhat odd approach to an Active Record Association.
On 14 February 2012 09:54, Marcos Korody <mfkorody-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have two different models. USERS and POSTS. Untill the present moment I > have a pretty straight forward relationship between them. > > I want a USER to also have the ability to set an original AUTHOR to the > content of his POST. > In other words, a POST belongs to a USER (submitter who''s posting some > content) and also belongs to an original AUTHOR(a person who''s post content > is originaly from).Not an odd request at all :-) You want to look online for "rails self referential associations" But essentially, in your User model add: belongs_to :author, :class_name => "User" and add a migration for the author_id field to be added to the users table. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Marcos Korody
2012-Feb-14 10:51 UTC
Re: A somewhat odd approach to an Active Record Association.
Well that does sound quite easy actually. Thank you for your prompt response, Pavling. Will try to apply the changes and I''ll come back with an outcome soon enough. Cheers! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/X1klJk4K3JAJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dave Aronson
2012-Feb-14 14:05 UTC
Re: A somewhat odd approach to an Active Record Association.
On Tue, Feb 14, 2012 at 05:05, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You want to look online for "rails self referential associations" > > But essentially, in your User model add: > belongs_to :author, :class_name => "User" > > and add a migration for the author_id field to be added to the users table.I think one of us (maybe more!) has misinterpreted what Marcos wanted. I think he wants to do that on Posts, not Users. That way, each User can post Posts from many different Authors, rather than each User being able to attribute things to only one Author ever. -Dave -- Dave Aronson: Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Michael Pavling
2012-Feb-14 14:16 UTC
Re: A somewhat odd approach to an Active Record Association.
On 14 February 2012 14:05, Dave Aronson <googlegroups2dave-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Tue, Feb 14, 2012 at 05:05, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> You want to look online for "rails self referential associations" >> >> But essentially, in your User model add: >> belongs_to :author, :class_name => "User" >> >> and add a migration for the author_id field to be added to the users table. > > I think one of us (maybe more!) has misinterpreted what Marcos wanted. > I think he wants to do that on Posts, not Users.Ah yes, sorry, I''ve galloped through and grabbed the wrong end of the stick. Here''s another gallop :-) # posts model belongs_to :user belongs_to :author, :class_name => "User" and add a migration for the author_id field to be added to the posts table. Same principle of changing the class_name for the association - just putting it on the right model this time! :-D -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.