Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:35:05+00:00 2026-05-24T16:35:05+00:00

I have four rails models called user, school, subscription and news_item. I a situation

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:35:06+00:00Added an answer on May 24, 2026 at 4:35 pm
    class NewsItem
      scope :news_for, lambda { |user|
        {
            :joins      => "INNER JOIN subscriptions on news_items.school_id = subscriptions.school_id",
            :conditions => "subscriptions.user_id = #{user.id}"
        }
      }
    end
    
    NewsItem.news_for(User.first)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Mongoid, Devise and Rails 3.1. I have four models: Students, Teacher, Parents
I have four tables I want to join and get data from. The tables
i have four tables user-question contains two columns: questionID, userID, the questions that the
In a rails application, I have a number of attributes for a model called
What is the difference between Ruby on Rails and Ruby? I have asked four
I have four input boxes. If the user fills the first box and clicks
I have a backbone application which, upon load, needs to fetch data from four
I am trying to get a simple page up that will have four links
I have this Rails code in a method in a controller. It takes four
I have four models: a, b, c, d here is what I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.