please help - complicated polymorphic association

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?

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

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