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 7842127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:14:54+00:00 2026-06-02T16:14:54+00:00

i created a messaging model where a user can send a private message to

  • 0

i created a messaging model where a user can send a private message to another user. however im not sure how to go about notifying the user he/she got a new message. does anyone have a way to go about doing this? or if there was a simple solution?

    def create
       @message = current_user.messages.build
       @message.to_id = params[:message][:to_id]
       @message.user_id = current_user.id
       @message.content = params[:message][:content]
       if @message.save
          flash[:success ] = "Private Message Sent"
       end
       redirect_to user_path(params[:message][:to_id])
    end

i can tell the sender that a private message was sent, but im not sure how i can notify the receiver a new private message was created.

help would be appreciated. thanks = )

  • 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-02T16:14:55+00:00Added an answer on June 2, 2026 at 4:14 pm

    First, you can improve your controller like this:

    def create
      @message = current_user.messages.new(params[:message])
    
      if @message.save
        flash[:message] = "Private Message Sent"
      end
      redirect_to user_path(@message.to_id)
    end
    

    Then, in your models:

    # app/models/message.rb
    class Message < ActiveRecord::Base
      belongs_to :user
      belongs_to :recipient, class_name: 'User', foreign_key: :to_id
      has_many :notifications, as: :event
    
      after_create :send_notification
    
    private
      def send_notification(message)
        message.notifications.create(user: message.recipient)
      end
    end
    
    # app/models/user.rb
    class User < ActiveRecord::Base
      has_many :messages
      has_many :messages_received, class_name: 'Message', foreign_key: :to_id
      has_many :notifications
    end
    
    # app/models/notification.rb
    class Notification < ActiveRecord::Base
      belongs_to :user
      belongs_to :event, polymorphic: true
    end
    

    This Notification model allows you to store a user’s notifications for different “events”. You can even store whether a notification has been read or not, or set an after_create callback in order to send an email to the notified user.

    The migration for this Notification model would be:

    # db/migrate/create_notifications.rb
    class CreateNotifications < ActiveRecord::Migration
      def self.up
        create_table :notifications do |t|
          t.integer :user_id
          t.string  :event_type
          t.string  :event_id
          t.boolean :read, default: false
    
          t.timestamps
        end
      end
    
      def self.down
        drop_table :notifications
      end
    end
    

    You can read about the Rails associations options here.

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

Sidebar

Related Questions

Am creating a messaging system model after facebook's messages. The message inbox is threaded,
I have created a messaging system which allows users to send messages to eachother.
I recently created a ajax based instant messaging application and after running for a
I want to create SOCKET chat(server + clients) with private messaging. When client write
Created Private Key & Self signed certficate in a Key Store keytool -genkey -alias
I have created a class library (assembly) that provides messaging, email and sms. This
I am trying to build an application that can detect if the Messaging application
Take facebook's private messaging system where you have to keep track of sender and
I'm new to Django (and databases in general), and I'm not sure how to
I have a messaging function that can be called several times on the same

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.