Taiwan Vincent
2011-Nov-24 14:55 UTC
How do I destroy all the posts when the board is destroyed?
I got a application like following. A board has many posts All posts belong to a board I try to destroy all the posts under a board when I destroy the board. My destroy function is following. def destroy @board = Board.find(params[:id]) @post = @board.posts @post.destroy @board.destroy respond_to do |format| format.html { redirect_to(boards_url) } format.xml { head :ok } end end However, it does not work. Could anyone tell me how to destroy all the posts when the board is destroyed? My source code is following. http://dl.dropbox.com/u/40209252/forum_demo.tar.gz -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Norbert Melzer
2011-Nov-25 17:23 UTC
Re: How do I destroy all the posts when the board is destroyed?
Am 24.11.2011 15:55, schrieb Taiwan Vincent:> Could anyone tell me how to destroy all the posts when the board is > destroyed?Take a look at the :dependent option for associations: <http://guides.rubyonrails.org/association_basics.html> HTH Norbert
Mukesh Singh
2011-Nov-25 17:35 UTC
Re: How do I destroy all the posts when the board is destroyed?
On Thu, Nov 24, 2011 at 8:25 PM, Taiwan Vincent < vincent.open.source.taiwan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I got a application like following. > > A board has many posts > All posts belong to a board > > I try to destroy all the posts under a board when I destroy the board. > My destroy function is following. > > def destroy > @board = Board.find(params[:id]) > @post = @board.posts > @post.destroy > @board.destroy > > respond_to do |format| > format.html { redirect_to(boards_url) } > format.xml { head :ok } > end > end > > However, it does not work. > Could anyone tell me how to destroy all the posts when the board is > destroyed? > > My source code is following. > http://dl.dropbox.com/u/40209252/forum_demo.tar.gz > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >1) class Board < ActiveRecord::Base has_many :posts, :dependent => :destroy end 2) posts.each do |post| post.destroy end 3) posts.delete_all -- Regards Mukesh Paras Singh -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Taiwan Vincent
2011-Nov-27 08:57 UTC
Re: How do I destroy all the posts when the board is destroyed?
Hi I got a solution. Here is my destroy function. Many thanks for the reply. def destroy @board = Board.find(params[:id]) @board.posts.destroy_all @board.destroy respond_to do |format| format.html { redirect_to(boards_url) } format.xml { head :ok } end end On Nov 26, 1:23 am, Norbert Melzer <timmel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> Am 24.11.2011 15:55, schrieb Taiwan Vincent: > > > Could anyone tell me how to destroy all the posts when the board is > > destroyed? > > Take a look at the :dependent option for associations: > > <http://guides.rubyonrails.org/association_basics.html> > > HTH > Norbert > > signature.asc > < 1KViewDownload-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Norbert Melzer
2011-Nov-27 09:32 UTC
Re: Re: How do I destroy all the posts when the board is destroyed?
Am 27.11.2011 09:57, schrieb Taiwan Vincent:> Hi I got a solution. Here is my destroy function. Many thanks for the > reply. > > def destroy > @board = Board.find(params[:id]) > @board.posts.destroy_all > @board.destroy > > respond_to do |format| > format.html { redirect_to(boards_url) } > format.xml { head :ok } > end > end > > On Nov 26, 1:23 am, Norbert Melzer <timmel...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> Am 24.11.2011 15:55, schrieb Taiwan Vincent: >> >>> Could anyone tell me how to destroy all the posts when the board is >>> destroyed? >> >> Take a look at the :dependent option for associations: >> >> <http://guides.rubyonrails.org/association_basics.html> >> >> HTH >> NorbertYou are quoting my post, where I give you a solution that handles everything with less coding effort in the model, where it belongs... But fiddling around with a hack in the controller, that simply shouldnt do such work. But maybe I am the one who is wrong? Bye Norbert