Question

I am trying to implement polymorphic association + self-referential associations.

The use case is:

I have two models

registered_user => registered users.
unregistered_friend => friends of registered users who have not registered.

I have a table

share (registered_user_id sharee_id sharee_type)

which stores data when

registered_user shares something with sharee

where sharee can be either registered_user or unregistered_friend

So basically what is the right way to go about it.

Should registered_user.rb have something like

has_many :share
has_many :share, :as => :sharee

Thanks!

Était-ce utile?

La solution

Need to specify different names for the associations of registered_users with share.

One named shares

has_many :shares,  :as => "sharee"

One named sharers or choose an appropriate name

has_many :sharers, :foreign_key => "registered_user_id", :class_name => "Share"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top