# .save and .destroy Accessing Different Tables?

**URL:** <https://www.ruby-forum.com/t/save-and-destroy-accessing-different-tables/136979>\
**Category:** Rails\
**Created:** [May 7, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/save-and-destroy-accessing-different-tables/136979 "2008-05-07T22:11:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 7, 2008, 10:11pm UTC](https://www.ruby-forum.com/t/save-and-destroy-accessing-different-tables/136979/1 "2008-05-07T22:11:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 7, 2008, 10:33pm UTC](https://www.ruby-forum.com/t/save-and-destroy-accessing-different-tables/136979/2 "2008-05-07T22:33:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lottario](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lottario](https://www.ruby-forum.com/u/lottario)\
**Post date:** [May 7, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/save-and-destroy-accessing-different-tables/136979/3 "2008-05-07T22:46:38Z")

</div>

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 🙂
