On 17 Aug 2010, at 13:37, Sateesh Kambhamapati wrote:
> I have doubts on Polymorphic Assosiations.
> On Which situations we are using these??
I used these recently for a pinboard. We have an internal system that contains
contact management, support tickets, products & services, billing etc. These
are all different represented by different active record models.
Users wanted to be able to pin arbitrary objects to an area of the site/app
that would be always available. For example the could have a couple of support
tickets, a contact record and maybe a few service records all accessible in the
same left hand div anywhere in the app.
I did this with the following model:
<snip>
class PinnedItem < ActiveRecord::Base
belongs_to :user
belongs_to :pinned, :polymorphic => true
validates :pinned_id, :uniqueness => {:scope => [:pinned_type, :user_id]}
if User.current
default_scope where(:user_id => User.current.id)
end
end
</snip>
> What are the advantages?
I couldn't say. This just seemed like the easiest way to accomplish this, that
is having a normal AR association but with arbitrary rather than a specific
class of objects.
This is the first time I've used polymorphic associations so if there any major
disadvantages that anyone's aware of I'd like to know.
Regards,
Jim
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.