Skip to content

Instantly share code, notes, and snippets.

@Sailias
Created April 29, 2010 14:55
Show Gist options
  • Save Sailias/383725 to your computer and use it in GitHub Desktop.
Save Sailias/383725 to your computer and use it in GitHub Desktop.
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic=>true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
###############################################
>> @article = Article.new
@article = Article.new
=> #<Article id: nil, created_at: nil, updated_at: nil>
>> @article.comments.build(:content=>"Hello")
@article.comments.build(:content=>"Hello")
=> #<Comment id: nil, created_at: nil, updated_at: nil, content: "Hello", commentable_id: nil, commentable_type: "Article">
>> @article.save
@article.save
=> true
>> @comment = @article.comments.last
@comment = @article.comments.last
=> #<Comment id: 2, created_at: "2010-04-29 14:54:07", updated_at: "2010-04-29 14:54:07", content: "Hello", commentable_id: 2, commentable_type: "Article">
>> @comment.update_attributes(:commentable_id=>"", :commentable_type=>"")
@comment.update_attributes(:commentable_id=>"", :commentable_type=>"")
=> true
>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment