Skip to main content
0 online
iwz iwz

NICE

http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations

Looks like I should be able to do this:

class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end

class Journal < ActiveRecord::Base
has_many :comments, :as => :commentable
end

Just have to rename generic_id and thread_type in the comments table to commentable_id and commentable_type, and do a quick update on the data to match the model names.

We'll see if it works!

iwz iwz

Works great! I'm running a migration that puts the correct data into the 2 new columns. It's going to take hours... gotta find a faster way to do it. haha

iwz iwz

Ok, that's better. 148 seconds to add the new columns, update them, and add the indexes. 270,000 rows in the comments table. Sheesh!

iwz iwz

And it looks like I'll be able to take advantage of Single Table Inheritance for the three similar models (News, Journals, ForumThreads), and at the same time take advantage of the Polymorphic Association with :as => :commentable for all the various models that accept comments.

NICE

iwz iwz

Yep, I found a bug. STI and Polymorphic Associations don't play together nicely all the time. nzkoz (rails core member) says it's a real live defect.

Details here http://rafb.net/paste/results/cVQNiO28.html

iwz iwz

Ok, not a bug. Whew. This is good news.

M
monsterEst. 2006

Ian, have you ever been to another country and got to a meeting and even thou it is in a language you don't understand... you can still pick out the brother that is really into their talk, exuberant, excited about the information. I have no clue what your talking about here, but I AM HAPPY 4U!! :-)

iwz iwz

haha, thanks for the encouragement :)

deanh77 deanh77Founder

sweet. I think this started in rails 1.1 which I need to get up to speed on...

Welcome Back to eZabel

It's been a while. Here's what's new.

eZabel Lore

A complete history of our community — stats, Hall of Fame, legendary threads, and more.

View the Lore →

Everything Preserved

All 225,969 pieces of content from 2000–2014 are here — forums, messages, journals, photos, polls, and events.

💎

Gems

Spot something you love — a legendary comment, a classic thread, a great photo? Log in and click the diamond icon to mark it as a Gem. Add a note about why it's special. The best stuff surfaces on the Gems page.