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?
Sandip Ransing
2009-Nov-02 10:27 UTC
Re: please help - complicated polymorphic association
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
<sixtimesnine-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
--
www.funonrails.com
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---