Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STI and polymorphs

I have problem with my code

class Post < ActiveRecord::Base
end

class NewsArticle < Post
  has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at'
end

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

And on attempt go get comments for some NewsArticle i see in logs something like

  Comment Load (0.9ms)   SELECT "comments".* FROM "comments" WHERE ("comments"."commentable_id" = 1 and "comments"."commentable_type" = 'Post') ORDER BY created_at

Strange that "commentable_type" = 'Post'. What's wrong?

PS: Rails 2.3.5 && ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10]

like image 893
Alexey Poimtsev Avatar asked Apr 08 '10 21:04

Alexey Poimtsev


4 Answers

The commentable_type field needs to store the name of the table that contains the data, once that row is loaded from the right table, the inherited type will be loaded from the type column on the Posts table.

So:

Here the comment points to the table that it comments on. The posts table, id 1

>> Comment.first
=> #<Comment id: 1, commentable_id: 1, commentable_type: "Post", body: "test", created_at: "2010-04-09 00:56:36", updated_at: "2010-04-09 00:56:36">

Then to load the NewsArticle, id 1 is loaded from posts, and the type there indicates a NewsArticle.

>> Comment.first.commentable
=> #<NewsArticle id: 1, type: "NewsArticle", name: "one", body: "body", created_at: "2010-04-09 00:55:35", updated_at: "2010-04-09 00:55:35">
>> Comment.first.commentable.class.table_name
=> "posts"

If commentable_type held "NewsArticle" it would have to look at the class to determine the table. This way it can just look to the table and worry about the type once it gets there.

like image 68
danivovich Avatar answered Nov 01 '22 14:11

danivovich


Take a look at the Polymorphic Associations section of ActiveRecord::Associations API. There is a little bit about using polymorphic associations in combination with single table inheritance. Following the second code example in that section I think this might be close to what you want

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

  def commentable_type=(sType)
   super(sType.to_s.classify.constantize.base_class.to_s)
  end
end

class Post < ActiveRecord::Base
  has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at'
end

class NewsArticle < Post
end
like image 39
Corey Avatar answered Nov 01 '22 14:11

Corey


def commentable_type=(sType) super(sType.to_s.classify.constantize.base_class.to_s) end

This method is returning class as Post, what to do if you want to store the inherited class Post as commentable_type?

like image 34
cnikolaou Avatar answered Nov 01 '22 15:11

cnikolaou


Good question. I had exactly the same problem using Rails 3.1. Looks the problem is not solved yet. Apparently, using polymorphic associations in combination with Single Table Inheritance (STI) in Rails is a bit complicated.

The current Rails documentation for Rails 3.2 gives this advice for combining polymorphic associations and STI:

Using polymorphic associations in combination with single table inheritance (STI) is a little tricky. In order for the associations to work as expected, ensure that you store the base model for the STI models in the type column of the polymorphic association.

In your case the base model would be "Post", i.e. "commentable_type" should be "Post" for all comments.

like image 1
0x4a6f4672 Avatar answered Nov 01 '22 16:11

0x4a6f4672