15 Mar '12, 5pm

#154 Polymorphic Association - RailsCasts:

class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as => :commentable #... end class Event < ActiveRecord::Base has_many :comments, :as => :commentable end # comments_controller def index @commentable = find_commentable @comments = @commentable.comments end def create @commentable = find_commentable @comment = @commentable.comments.build(params[:comment]) if @comment.save flash[:notice] = "Successfully created comment." redirect_to :id => nil else render :action => 'new' end end private def find_commentable params.each do |name, value| if name =~ /(.+)_id$/ return $1.classify.constantize.find(value) end end nil end

Full article: http://railscasts.com/episodes/154-polymorphic-associatio...

Tweets

Railscasts - Polymorphic Association:

railscasts.com 09 Mar '12, 10pm

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

Railscasts - Auto-Complete Association:

railscasts.com 07 Mar '12, 2am

Usually a select menu is used for setting a belongs_to association, but in this episode I will show you how to use a text ...