ActiveRecord has_many and polymorphic

Posted by leomayleomay on Stack Overflow See other posts from Stack Overflow or by leomayleomay
Published on 2010-05-10T11:47:07Z Indexed on 2010/05/10 12:54 UTC
Read the original article Hit count: 189

Filed under:

I've came into a problem while working with AR and polymorphic, here's the description,

class Base < ActiveRecord::Base; end
class Subscription < Base

set_table_name :subscriptions
has_many :posts, :as => :subscriptable

end

class Post < ActiveRecord::Base

belongs_to :subscriptable, :polymorphic => true

end

in the console,

>> s = Subscription.create(:name => 'test')
>> s.posts.create(:name => 'foo', :body => 'bar')

and it created a Post like:
#<Post id: 1, name: "foo", body: "bar", subscriptable_type: "Base", subscriptable_id: 1, created_at: "2010-05-10 12:30:10", updated_at: "2010-05-10 12:30:10">

the subscriptable_type is Base but Subscription, anybody can give me a hand on this?

© Stack Overflow or respective owner

Related posts about ruby-on-rails