Polymorphic associations and acts_as_xxx

July 26, 2009 at 3:00 pm Leave a comment

There are many plugins that allow you to use Rails’ powerful polymorphic relationship. Basically, polymorphic relationship allows you to create for example a Comments module and link to it from any of your classes by using “acts_as_commentable” declaration. How Comment class will not which object it gets linked to? It simply stores 2 fields in the database – xxx_id and xxx_type.

And what if you want to get an object (for example a post) on which somebody commented? It’s actually quite simple. If @comment stores your comment, then @comment.commentable will be the object on which the comment was made. This principle will work with any other acts_as plugin. For acts_as_voteable, you will need to access vote.voteable, etc.

Advertisements

Entry filed under: Uncategorized.

Rails books in free access Ruby on Rails screencast / course

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent posts

Starting to learn Rails?

Kindle

Get Kindle - the best e-book reader, that I personally use, and the only one that you can read on the beach - very useful: Kindle Wireless Reading Device (6" Display, Global Wireless, Latest Generation)

%d bloggers like this: