model user's message in rails 3

后端 未结 3 1021
醉梦人生
醉梦人生 2020-12-08 12:09

I have built the following model to handle user\'s message exchange:

 create_table \"messages\", :force => true do |t|
    t.integer  \"source_id\"
    t.         


        
相关标签:
3条回答
  • 2020-12-08 12:40

    this is a nice problem! I would model that to compare as closely as possible to the e-mail model. So a message always belongs to a single user, and it was either sent or received.

    In short:

     create_table "messages", :force => true do |t|
        t.integer  :user_id
        t.string   :subject
        t.string   :body
        t.boolean  :sent
      end
    

    And the model would like:

    class Message < ActiveRecord::Base
      belongs_to :user
    
      scope :sent, where(:sent => true)
      scope :received, where(:sent => false)
    
    end
    

    And in the user:

    class User    
      has_many :messages
    end
    

    You would then simply be able to query all sent messages by

    user.messages.sent
    

    and the received messages

    user.messages.received
    

    Sending a message does become a bit more complicated then:

    class Message
    
      def send_message(from, recipients)
        recipients.each do |recipient|
          msg = self.clone
          msg.sent = false
          msg.user_id = recipient
          msg.save
        end
        self.update_attributes :user_id => from.id, :sent => true
      end   
    end
    

    or something along those lines: you copy the message and attach it to all recipients, and lastly make the original message the sent message.

    This way each user has total control over the message.

    Possible improvements:

    • also keep an explicit reference to the sender and receiver(s) in the message, to be able to allow replies and stuff
    • instead of working with a single boolean, maybe allow working with folders?

    Hope this helps.

    0 讨论(0)
  • 2020-12-08 12:54

    You can add two booleans to mark the message as deleted for both sender and receiver. Then after setting either of them check if the message can be deleted permanently.

    Example:

    create_table "messages", :force => true do |t|
      t.boolean :sender_deleted
      t.boolean :receiver_deleted
    end
    

    And in model:

    class Message
      def self.delete_message(id)
        m = Message.find(id)
        m.destroy if m.sender_deleted && m.receiver_deleted
      end
    end
    
    0 讨论(0)
  • 2020-12-08 12:57

    You can nullify on a deleted record with :dependent=>:nullify

    has_many :sent_messages, :class_name=> 'Message', :foreign_key=>'source_id', :dependent=>:nullify
    has_many :recieved_messages, :class_name=> 'Message', :foreign_key=>'destination_id', :dependent=>:nullify
    

    You'll need to handle when displaying the message that the sender/receiver of the message has been deleted, since the sender_id or destination_id will be null, but the message will stay intact.

    0 讨论(0)
提交回复
热议问题