# Please help - complicated polymorphic association

**URL:** <https://www.ruby-forum.com/t/please-help-complicated-polymorphic-association/178271>\
**Category:** Rails\
**Created:** [November 1, 2009, 9:59pm UTC](https://www.ruby-forum.com/t/please-help-complicated-polymorphic-association/178271 "2009-11-01T21:59:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thelemcke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thelemcke](https://www.ruby-forum.com/u/thelemcke)\
**Post date:** [November 1, 2009, 9:59pm UTC](https://www.ruby-forum.com/t/please-help-complicated-polymorphic-association/178271/1 "2009-11-01T21:59:50Z")

</div>

I am trying to build a shared-appointment system, where users can  
subscribe to appointments and be updated whenever changes are made to  
them. I have three objects in this system, appointments, users, and  
subscribers. Subscribers are a polymorphic object like so:  
class Subscriber \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :subscribable, :polymorphic =\> true  
end

The tricky part is that users already own appointments, so I can’t  
create a simple users.appointments association. I can find users by  
calling appointment.subscribers, but I need help getting the reverse  
to work (users.subscribers.appointments) I can use named\_scope to  
only grab appointment subscriber objects, but how can I make it  
automatically link to the appointments, not the subscriber objects?

---

<div class="post-metadata">

**Author:** ![thelemcke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/97f17d/32.png) [@thelemcke](https://www.ruby-forum.com/u/thelemcke)\
**Post date:** [November 2, 2009, 11:48am UTC](https://www.ruby-forum.com/t/please-help-complicated-polymorphic-association/178271/2 "2009-11-02T11:48:59Z")

</div>

Hi

As subscriber model is polymorphic and as you  
are referring “subscribers.appointments”  
It means, you are having following association in appointment model.

class Appointment \< ActiveRecord::Base  
has\_many :subscribers, :as =\> : subscribable  
end

Here goes solution

class User \< ActiveRecord::Base  
has\_many :subscribers

def appointments  
self. subscribers.of\_appointment\_type.collect { |s| s.subscribable }  
end  
end

defined “of\_appointment\_type” as a named\_scope in Subscriber model

class Subscriber \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :subscribable, :polymorphic =\> true

named\_scope :of\_appointment\_type, :conditions =\> [ "subscribable\_type =  
? ",  
“Appointment”]

end

On Mon, Nov 2, 2009 at 1:51 AM, theLemcke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I am trying to build a shared-appointment system, where users can  
> subscribe to appointments and be updated whenever changes are made to  
> them. I have three objects in this system, appointments, users, and  
> subscribers. Subscribers are a polymorphic object like so:  
> class Subscriber \< ActiveRecord::Base  
> belongs\_to :user  
> belongs\_to :subscribable, :polymorphic =\> true

It means you have subscribable\_id and subscribable\_type fields in table

> end

> The tricky part is that users already own appointments, so I can’t  
> create a simple users.appointments association. I can find users by  
> calling appointment.subscribers, but I need help getting the reverse  
> to work (users.subscribers.appointments) I can use named\_scope to  
> only grab appointment subscriber objects, but how can I make it  
> automatically link to the appointments, not the subscriber objects?
> 
> >

## Sandip

> **[Fun On Rails](http://funonrails.com/)**
>
> Journal of a Web Developer #ruby #rails #JS
