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

  • Home
  • SEARCH
  • 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 9251715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:46:17+00:00 2026-06-18T10:46:17+00:00

I have a Rails 3.2 application with a user model. I want to add

  • 0

I have a Rails 3.2 application with a user model. I want to add a notification mechanism that can be triggered by the following use cases:

  • Liking someone’s profile
  • Commenting on someone’s profile
  • Following someone

In all cases, one user generates a notification by his behavior that the other user will receive. So, there is a sender and a recipient of notifications.

I am now pondering how to structure my associations. So far, my models looks like the following:

Notification.rb

attr_accessible :content, :read_at, :recipient_id, :sender_id

belongs_to :sender,     class: :user
belongs_to :recipient,  class: :user

User.rb

has_many :notifications, as: :recipient, dependent: :destroy, foreign_key: :recipient_id
has_many :notifications, as: :sender, dependent: :destroy, foreign_key: :sender_id

This pseudocode shall only help understand what I need – what confuses me a lot is that I refer to the user model twice inside the notification model, and that a user has many notifications in two different ways.

So, my questions are:

  • How would you adjust the above associations?
  • What would you call them?
  • How will I be able to call all sent notifications and all received notifications on a user model without having to write scopes?
  • Is it the right approach at all, or should I use a join table somewhere two avoid referencing the user twice?

Thanks!

Solution

To put Shane’s solution into words, this is what the models have to look like. It was really much easier than I thought. I assumed I would have to do some magic here – but Rails tricked me again with its stunning simplicity! Well, that’s why I like it so much.

Thanks a lot, Shane!

Notification.rb

attr_accessible :content, :read_at, :recipient, :recipient_id, :sender, :sender_id

belongs_to :sender, class_name: "User"
belongs_to :recipient, class_name: "User"

User.rb

has_many :received_notifications, class_name: "Notification", foreign_key: "recipient_id", dependent: :destroy
has_many :sent_notifications, class_name: "Notification", foreign_key: "sender_id", dependent: :destroy
  • 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-06-18T10:46:18+00:00Added an answer on June 18, 2026 at 10:46 am

    This is completely normal. Here is example of the same pattern used in a Redmine, a popular open source app:

    https://github.com/edavis10/redmine/blob/master/app/models/workflow_rule.rb#L23-L24

    As for needing a method that gets both sent and received notifications, the answer here works well for basic needs.

    Rails Model has_many with multiple foreign_keys

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails Application that is having User , Role , Group models.
I'm building Rails application and I want to have user registration/login functionality. I also
I'd like to add several different user types to my rails application. I have
I have a rails application with an administration interface that allows a user CRUD
I have a rails application with User and Machine data models, as well as
I have a simple rails application were book belongs_to user and user has_many book.
I have a Rails application that accepts file uploads of CSV files. When developing
I have a rails application that about 3 years old, and I'm having a
I have a Rails application that uses ActiveRecordStore for sessions. I need a PHP
I have a Rails application that lists information about local services. My objectives for

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.