views:

76

answers:

1

Hello, i have some issue with database.

Okay, i have two models -> Page and Item. Page for displaying some content. Item -> this is item discription.

So, i work on small ecommerce shop.

Okay, all of this models can have some comments.

So, this is my Comments model at this moment:

Comments ->

string : id
text : body
integer : page_id
integer : item_id

So when some one add comment to page -> page_id will be filled with current Page id.

And if some one add comment to item -> item_id will be filled.

Okay, i know what the best way is to create STI or Polymorphic assoc, but does i really need this way for my situation?

Sorry for my bad english, i'm from Russia.=)

+1  A: 

Although the way you have your tables and models works for you, you should consider using polymorphic. You don't have to use it, but I believe this is the most recommended way.

To use the polymorphic associations you'll need to change your models:

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

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

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

and your comments table, that will have this columns now:

integer : id # should be integer, not string
text    : body
integer : commentable_id
string  : commentable_type 

Hope it helps...

j.