Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Delete child records: has_many *, :dependent => :destroy

$
0
0
I'm adding records to a database from a chat server process running outside of my rails app. There are chat_rooms and chat_messages which belong to chat rooms. I'm trying to set it up so that when I delete a chat_room (from within rails), all of it's chat_messages are deleted as well, which I thought would be a sinch, but it doesn't seem to be working.

The primary key is effectively 'room_hash' rather than ID, because the process that adds messages to the database only knows the room_hash, not the ID of that room. I have the feeling that's where things must get a little complicated... I tried playing with primary key stuff to no avail.

(chat_room.rb)
class ChatRoom < ActiveRecord::Base
  validates :room_hash, :uniqueness => true
  has_many :chat_messages, :foreign_key => :room_hash, :dependent => :destroy  # damn I don't know how to pull this off outside of rails
  .
  .
  .
end



(chat_room.rb)
class ChatMessage < ActiveRecord::Base
  belongs_to :chat_room, :foreign_key => :room_hash
  
  def decrypted_msg
    self.msg.tr('+/', '-_').unpack('m')[0]
  end
end



So when I try to delete a chat_room, it doesn't decrease the ChatMessages.count amount. What else do I need to do to complete the relationship?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>