Polymorphic association

36 views
Skip to first unread message

Rodrigo Ruiz

unread,
Jan 27, 2012, 11:44:41 AM1/27/12
to rubyonra...@googlegroups.com
Hi, I want to do a polymorphic association like that:

class User
  has_many :my_posts, :as => :owner
end

class Post
  belongs_to, :owner, :polymorphic => true
end


meaning that I want to do:
@user = User.new
@user.my_posts (and that would return Post objects)

also
@post = Post.new
@post.owner (this would return a User object)

How can I do that?

Thanks in advance.

Greg Akins

unread,
Jan 27, 2012, 11:52:47 AM1/27/12
to rubyonra...@googlegroups.com

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


--
Greg Akins
http://twitter.com/akinsgre

Greg Akins

unread,
Jan 27, 2012, 11:53:39 AM1/27/12
to rubyonra...@googlegroups.com
On Fri, Jan 27, 2012 at 11:52 AM, Greg Akins <angr...@gmail.com> wrote:
> On Fri, Jan 27, 2012 at 11:44 AM, Rodrigo Ruiz <rodrig...@gmail.com> wrote:
>> Hi, I want to do a polymorphic association like that:

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

Rodrigo Ruiz

unread,
Jan 27, 2012, 12:05:47 PM1/27/12
to rubyonra...@googlegroups.com
I did, and it goes like:
@commentable.comments
but I want something like:
@commentable.my_comments
and not change the Comment model name

--
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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Greg Akins

unread,
Jan 27, 2012, 12:08:31 PM1/27/12
to rubyonra...@googlegroups.com
On Fri, Jan 27, 2012 at 12:05 PM, Rodrigo Ruiz <rodrig...@gmail.com> wrote:
> I did, and it goes like:
>
> @commentable.comments
>
> but I want something like:
>
> @commentable.my_comments
>
> and not change the Comment model name

def my_comments
return comments
end

Rodrigo Ruiz

unread,
Jan 27, 2012, 12:15:01 PM1/27/12
to rubyonra...@googlegroups.com
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
end

Dheeraj Kumar

unread,
Jan 27, 2012, 12:20:26 PM1/27/12
to rubyonra...@googlegroups.com
You can use :class_name and :foreign_key if that's what you're looking for.


Dheeraj Kumar

Rodrigo Ruiz

unread,
Jan 27, 2012, 12:22:47 PM1/27/12
to rubyonra...@googlegroups.com
Yes, exactly! Does that :class_name works for has_many?

I'll try that now anyway.

Thank you

Dheeraj Kumar

unread,
Jan 27, 2012, 12:25:57 PM1/27/12
to rubyonra...@googlegroups.com
Sure does :D


Dheeraj Kumar

Rodrigo Ruiz

unread,
Jan 27, 2012, 1:28:04 PM1/27/12
to rubyonra...@googlegroups.com
That didn't work.

class Post
  belongs_to :owner, :polymorphic => true
end

class User
  has_many :my_posts, :class_name => 'Post', :as => :owner
end

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

Benjamin Iandavid Rodriguez

unread,
Jan 27, 2012, 1:37:42 PM1/27/12
to rubyonra...@googlegroups.com
You might want to take a look at the rails guides on active record associations

http://guides.rubyonrails.org/association_basics.html

2012/1/27 Rodrigo Ruiz <rodrig...@gmail.com>

Rodrigo Ruiz

unread,
Jan 27, 2012, 1:39:03 PM1/27/12
to rubyonra...@googlegroups.com
I did, also, it gives me the error: "SQLite3::SQLException: no such column: protests.owner_type:"

Rodrigo Ruiz

unread,
Jan 27, 2012, 1:53:18 PM1/27/12
to rubyonra...@googlegroups.com
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?

Mohamad El-Husseini

unread,
Jan 27, 2012, 2:50:22 PM1/27/12
to rubyonra...@googlegroups.com
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.

Rodrigo Ruiz

unread,
Jan 27, 2012, 3:42:20 PM1/27/12
to rubyonra...@googlegroups.com
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.

--
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/-/SaT5HAQHi1EJ.

Mohamad El-Husseini

unread,
Jan 27, 2012, 4:12:31 PM1/27/12
to rubyonra...@googlegroups.com
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


Rodrigo Ruiz

unread,
Jan 27, 2012, 4:18:48 PM1/27/12
to rubyonra...@googlegroups.com
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.


--
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/-/hwFc1eURfFwJ.

Colin Law

unread,
Jan 27, 2012, 4:26:02 PM1/27/12
to rubyonra...@googlegroups.com
On 27 January 2012 21:18, Rodrigo Ruiz <rodrig...@gmail.com> wrote:
> 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.

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.

Colin

Mohamad El-Husseini

unread,
Jan 27, 2012, 4:34:27 PM1/27/12
to rubyonra...@googlegroups.com
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...


Mohamad El-Husseini

unread,
Jan 27, 2012, 4:37:59 PM1/27/12
to rubyonra...@googlegroups.com
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.

Colin Law

unread,
Jan 27, 2012, 4:42:57 PM1/27/12
to rubyonra...@googlegroups.com
On 27 January 2012 21:26, Colin Law <cla...@googlemail.com> wrote:
> On 27 January 2012 21:18, Rodrigo Ruiz <rodrig...@gmail.com> wrote:
>> 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.
>
> 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.

When I say work through it I mean actually entering the code and
getting it to work of course.

Colin

Rodrigo Ruiz

unread,
Jan 27, 2012, 8:22:03 PM1/27/12
to rubyonra...@googlegroups.com
When you say documentation you mean the Rails Guide?

On Fri, Jan 27, 2012 at 7:37 PM, Mohamad El-Husseini <hussei...@gmail.com> wrote:
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.

--
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/-/gpfZEWYqkawJ.

Mohamad El-Husseini

unread,
Jan 28, 2012, 6:11:21 AM1/28/12
to rubyonra...@googlegroups.com
I mean that, and any other material you are using to learn. Read it properly and understand it. If there's something you don't understand, ask away!
Reply all
Reply to author
Forward
0 new messages