I have four rails models called user, school, subscription and news_item. I a situation where a user subscribes to school to get news notification from that school, how is it possible to display all the news_items that a user is subscribed to.
my user model has this
belongs_to :school
has_many :schools, :through => :subscriptions
has_many :subscriptions
def subscribe_to(school)
subscribe = subscriptions.build(:school_id => school.id)
if !subscribe.save
logger.debug "you have already subscribed to '#{school.name}'"
end
end
after_create :create_school_subscription
def create_school_subscription
school = self.school_street
self.subscriptions.build(:school_id => school.id)
end
and my subscription model has
belongs_to :user
belongs_to :school, :class_name => 'School'
validates_uniqueness_of :school_id, :scope => :user_id
validates_presence_of :user_id, :school_id
my school model has
has_many :users
has_many :news_items
finally my news_item model has
belongs_to :school
with all this put in place how can i get all the news a user is subscribed to through the school
1 Answer