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.

On 14 February 2012 09:54, Marcos K. [email protected] 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 :slight_smile:

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.

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!

On Tue, Feb 14, 2012 at 05:05, Michael P. [email protected]
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 A.: 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

On 14 February 2012 14:05, Dave A.
[email protected] wrote:

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 :slight_smile:

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! :smiley: