model user's message in rails 3

后端 未结 3 1041
醉梦人生
醉梦人生 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: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.

提交回复
热议问题