.save and .destroy Accessing Different Tables?

I'm new to Ruby and to Rails, so this whole problem is probably just bad
coding on my part, but on the same object, .save and .destroy are
accessing separate database tables, one which exists (which I want them
to access), and one which doesn't. The code causing issues:

def message_sending
    @message.sent_at = Time.now
    if @message.update_attributes(params[:message])

      hold = Object.new
      hold = @message.recipients.downcase.split(",")

      i=0
      trfa = false
      if @message.title.length > DB_STRING_MAX_LENGTH
        flash[:notice] = "Title too long!"
        @message.destroy
      elsif @message.recipients.length > DB_STRING_MAX_LENGTH
        flash[:notice] = "Too many recipients!"
        @message.destroy
      elsif @message.body.length > DB_TEXT_MAX_LENGTH
        flash[:notice] = "Body text too long!"
        @message.destroy
      else
        while (i<hold.length)
          if User.find_by_screen_name(hold[i]) == nil
            flash[:notice] = "Recipient " + hold[i] + " doesn't exist!"
            @message.destroy
            break
          else
            if trfa == false
              @message.recipients =
User.find_by_screen_name(hold[i]).get_user_id.to_s
              trfa = true
            else
              @message.recipients = @message.recipients + "," +
User.find_by_screen_name(hold[i]).get_user_id.to_s
            end
            i=i+1
          end
          @message.sender = find_user_and_messages.get_user_id.to_s
          @message.save
          redirect_to hub_url
          flash[:notice] = "Message Sent!"
        end
      end
    end
  end

Using @message.save anywhere in that code will access the correct
"messages" table. However, an @message.destroy will attempt to access
the nonexistent "messages_users" table. I'm completely stumped as to
why .save can access the right table when .destroy can't. Any help
would be much appreciated.

This is probably due to cascading deletes setup by your model
association

glasswing28 wrote:

This is probably due to cascading deletes setup by your model
association

That was precisely the problem! Thanks very much for your help,
everything is working as it should now :slight_smile: