0
votes

I am new to association.any one explain me where to use polymorphic association and its purpose .

class Picture < ActiveRecord::Base belongs_to :imageable, :polymorphic => true end

class Employee < ActiveRecord::Base has_many :pictures, :as => :imageable end

class Product < ActiveRecord::Base has_many :pictures, :as => :imageable end

Thanks in advance.

1

1 Answers

0
votes

polymorphic association is used when the parent or child object class is not known.

eg

A has_many :cs, :as => resource, :dependent => :destroy
B has_many :cs, :as => resource, :dependent => :destroy

C belongs_to :resource, :polymorphic => true

here, C may either belong to A or B.

see http://guides.rubyonrails.org/association_basics.html#polymorphic-associations for more information.