Polymorphic association

Hi, I want to do a polymorphic association like that:

class User

has_many :my_posts, :as => :owner


class Post

belongs_to, :owner, :polymorphic => true


meaning that I want to do:

@user = User.new

@user.my_posts (and that would return Post objects)


@post = Post.new

@post.owner (this would return a User object)

How can I do that?

Thanks in advance.

It looks like you're describing a standard has_many relationship. Are
you implying that Post will be extended to be different classes?

Try this railscast http://railscasts.com/episodes/154-polymorphic-association

I did, and it goes like:


but I want something like:


and not change the Comment model name

def my_comments
    return comments

I want this because I’m going to have to has_many associations to comments (to follow to same example):

class User

has_many :my_comments

has_many :comments_that_i_follow


You can use :class_name and :foreign_key if that’s what you’re looking for.

Dheeraj Kumar

Yes, exactly! Does that :class_name works for has_many?

I’ll try that now anyway.

Thank you

Sure does :smiley:

Dheeraj Kumar

That didn’t work.

class Post

belongs_to :owner, :polymorphic => true


class User

has_many :my_posts, :class_name => ‘Post’, :as => :owner


I can’t do: Post.first.owner or User.first.my_posts

You might want to take a look at the rails guides on active record associations


I did, also, it gives me the error: “SQLite3::SQLException: no such column: protests.owner_type:”

Hey, I just manage to do what I wanted, but I had to add a owner_type column on my Posts migration with the content:

:owner_type => ‘User’

Thanks for everyone, and is there a better way than saving another varchar(255) to my database?

I suggest you pick up a book and start following a tutorial. Otherwise you’ll spend most of your time here waiting for people to answer your questions. This becomes frustrating very quickly.

Trust me, it may seem easier to come here and ask extremely simple questions (the answers of which are obvious in the docs) but it’s not. In the long term, you’ll be thankful you took the extra few minutes to read and understand the docs. The books are extremely helpful and will answer most of your basic questions. I started learning Ruby and RoR about two weeks ago, and I’m very glad I took the time to read through and complete a tutorial.

Just visit amazon.com and type rails books and you’ll have a great selection to choose from. Also, try this, which I started with: http://ruby.railstutorial.org/chapters/static-pages --avoid the shortcuts and don’t rush. It pays off in the end.

I read the Rails Tutorial book, but I does not teach anything related to customizing that much, thats why I’m constantly search google and asking questions here (when I don’t find it on google).

I’m still searching google, but couldn’t find my answer for the current question.

Well, frankly, if I were you, I would read the documentation again. It doesn’t seem like you know what a polymorphic association is. You described a standard has_many relationship.

Polymorphic association is when one model belongs to several different models. For example, to use the Rails casts episode, a comment can belong to a Photo, a Post, or a Product.

From what you have written so far, it seems like you want a standard has_many and belongs_to association.

User has_many :posts will let you do @user.posts, and a Post belongs_to :user will let you do post.user. If you want customize the name of the association, as in post.author, then I think you need to use the :class_name

Could you recommend me a more complete book that I can read so I understand the basics?

I really feel like I don’t right now.

Thank you.

When you say you have read the rails tutorial book is that the one
that goes with railstutorial.org? If so then reading it is not
enough. Work right through it doing all the examples and so on. Make
sure you understand every line of code. You may think that the
application it develops is not what you want but that does not matter,
it will show you a wide range of techniques. Make sure that you have
installed the version of rails that matches that used by any tutorial
you use.


Don’t let that daunt you. I was in the same boat as you were not long ago. I’ll tell you what I did:

Completed http://ruby.railstutorial.org/ – twice! It does an awesome job of explaining things.
Rails for Zombies I, a screen cast series on codeschool.com (I think–you’ll have to google it).
Rails Casts
Rails Guides

I actually started with Programming Ruby 1.9, the Pragmatic Programmers Guide. I didn’t finish it. I read enough to get the basics of Ruby syntax. Knowing the Ruby syntax is essential since, to me, it looked nothing like anything I’ve seen before. Bracket were often omitted, and so were semi-colons, blocks, etc… understanding the syntax made understanding easier.

After getting through 200 pages, I switched to Agile Web Development with Rails 4th Ed. Frankly, I thought it was a waste of money at that point. It just told you to do stuff without explaining much of what you were doing. Especially when it came to testing. I quit and switched to Rails Tutorial. I don’t think it’s beginner friendly.Rails Tutorial I thought was brilliant. But it was hard to follow because of some issues. There was no mention that Heroku did not support SQLite for example. But the book does an awesome job of explaining the basics. Read it carefully. And it’s free.

I’m planning on Reading Rails AntiPatterns and The Rails 3 Way next…

I’ll second what Colin said. I’ll also emphasize the need to “hold your horses”. There’s nothing more frustrating than having to wade through a ton of stuff before you start building something. But it’s absolutely worth it, and it will save you a ton of time in the long run.

If I had spent 10% of my time reading documentation, I would have saved the other 90% that I wasted editing lines of code, scanning hurriedly over tutorials, asking a ton of questions, etc… it really is true. It’s frustrating at first, but it pays off and saves you a ton of time in the long term.