Hi, I have problems copying and deleting rows in tables with relations. In the category''s index page, when copy link is clicked, it should copy and create a new data record with the data of category, books, titles and prices that are in the original category. But instead, I only managed to copy only the category data and book data of the original category. In the book''s index page, when copy link is clicked, it should copy and create a new data record with the data of book, titles and prices that are in the original book. Please help. The following are my models and controller. But instead, I only managed to copy only the book data, title data and price data of the original book. Below are my models, controllers and view. Very in need of help. Thanks. ..................Models..................... ....category.rb....... set_table_name "category" has_many :books, :dependent => :delete_all ....book.rb........ set_table_name "book" belongs_to :category has_many :titles, :dependent => :delete_all has_many :prices, :dependent => :delete_all ....title.rb.......... set_table_name "title" belongs_to :book ....price.rb....... set_table_name "price" belongs_to :book .....category controller............ def copy_all @category = Category.find(params[:id],:include => :books ) @new_category = @category.clone @category.books.each do |t| @new_category.books << t.clone end if @new_category.save redirect_to :action => "index" flash[:notice] = "Category copied!" else render :action => "index" flash[:notice] = "Category was not copied!" end end .......book controller............. def copy_all @book = book.find(params[:id],:include => [:titles, :prices]) @new_book = @book.clone @book.titles.each do |t| @new_book.titles << t.clone end @book.prices.each do |t| @new_book.prices << t.clone end if @new_book.save redirect_to :action => "index" flash[:notice] = "Book copied! " else render :action => "index" flash[:notice] = "Book was not copied! " end end end ...........(category''s view) index.rhtml............ <% for category in @categories %> <tr> <td><%=h category.name %></td> <td><%= link_to ''Show'', :action => "show", :id => category.id %></td> <td><%= link_to ''Edit'', :action => "edit", :id => category.id %></td> <td><%= link_to ''Copy'', :action => "copy_all", :id => category.id %></td> <td><%= link_to ''Destroy'', { :action => "delete", :id => category.id }, {:post => true, :confirm => ''Are you sure you want to delete?''} %></td> </tr> <% end %> ..............(book''s view)index.rhtml..................... <% for book in @books %> <tr> <td><%=h book.name %></td> <td><%= link_to ''Show'', :action => "show", :id => book.id %></td> <td><%= link_to ''Edit'', :action => "edit", :id => book.id %></td> <td><%= link_to ''Copy'', :action => "copy_all", :id => book.id %></td> <td><%= link_to ''Destroy'', { :action => "delete", :id => book.id }, {:post => true, :confirm => ''Are you sure you want to delete?''} %></td> </tr> <% end %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-04 08:25 UTC
Re: Problems with copying and deleting with table relations
user splash wrote:> > > ..............(book''s view)index.rhtml..................... > > <% for book in @books %> > <tr> > <td><%=h book.name %></td> > <td><%= link_to ''Show'', :action => "show", :id => book.id %></td> > <td><%= link_to ''Edit'', :action => "edit", :id => book.id %></td> > <td><%= link_to ''Copy'', :action => "copy_all", :id => book.id > %></td> > <td><%= link_to ''Destroy'', { :action => "delete", :id => book.id }, > {:post => true, :confirm => ''Are you sure you want to delete?''} %></td> > </tr> > <% end %>I found the answer to deleting. I have to put :through in the category model. Example: ....category.rb....... set_table_name "category" has_many :books, :dependent => :delete_all has_many :titles, :through => books, :dependent => :delete_all has_many :prices, :through => books, :dependent => :delete_all ........................... But I am still stuck at the copying part. Can someone please help me? Thanks. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-07 03:39 UTC
Re: Problems with copying and deleting with table relations
user splash wrote:> user splash wrote: >> >> >> ..............(book''s view)index.rhtml..................... >> >> <% for book in @books %> >> <tr> >> <td><%=h book.name %></td> >> <td><%= link_to ''Show'', :action => "show", :id => book.id %></td> >> <td><%= link_to ''Edit'', :action => "edit", :id => book.id %></td> >> <td><%= link_to ''Copy'', :action => "copy_all", :id => book.id >Hi, I managed to copy using the following. But there is problem. The category_id, a foreign key in both the title and the price table, is getting the wrong id. It is getting category_id "1", id of the original copy of category instead of category_id "2", the id of the newly copied version of the original category. I really need help. Can someone please help me with this? Thanks ........category model........ . . . . def deep_clone result = self.clone self.books.each { |b| result.books << b.deep_clone} result end .......book model............. . . . def deep_clone result = self.clone self.titles.each { |t| result.titles << t.clone} self.prices.each { |p| result.prices << p.clone} result end ......category controller........ def copy_all @category = Category.find(params[:id],:include => [:books, :titles, :prices]) @new_category = @category.deep_clone if @new_category.save redirect_to :action => "edit_copied" flash[:notice] = "Category copied! " else render :action => "index" flash[:notice] = "Category was not copied! " end end ......book controller.......... def copy_all @book = Book.find(params[:id],:include => [:titles, :prices]) @new_book = @book.deep_clone if @new_book.save redirect_to :action => "edit_copied_book" flash[:notice] = "Book copied! " else render :action => "index" flash[:notice] = "Book was not copied! " end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-08 02:51 UTC
Re: Problems with copying and deleting with table relations
user splash wrote:> > > ..............(book''s view)index.rhtml..................... >action => "edit", :id => book.id %></td>> <td><%= link_to ''Copy'', :action => "copy_all", :id => book.id >> > > Hi, > > I managed to copy using the following. But there is problem. The > category_id, a foreign key in both the title and the price table, is > getting the wrong id. It is getting category_id "1", id of the original > copy of category instead of category_id "2", the id of the newly copied > version of the original category. >Please help. Quite urgent. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
In the data model of your original post, title and price belong to book but have no relation defined for category. has that changed? Where is category_id being set for them? On Apr 7, 10:51 pm, user splash <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> user splash wrote: > > > ..............(book''s view)index.rhtml..................... > > action => "edit", :id => book.id %></td> > > > <td><%= link_to ''Copy'', :action => "copy_all", :id => book.id > > > Hi, > > > I managed to copy using the following. But there is problem. The > > category_id, a foreign key in both the title and the price table, is > > getting the wrong id. It is getting category_id "1", id of the original > > copy of category instead of category_id "2", the id of the newly copied > > version of the original category. > > Please help. Quite urgent. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-10 01:57 UTC
Re: Problems with copying and deleting with table relations
AndyV wrote:> In the data model of your original post, title and price belong to > book but have no relation defined for category. has that changed? > Where is category_id being set for them? > > On Apr 7, 10:51 pm, user splash <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Hi, I have made some changes to the category model as shown below. The other models are still the same as posted in the original post. Is there other steps I''ve missed out? I my item and price tables, there are two foreign keys, category_id and book_id. Is there a way to solve the problems of my third post? Please help. Thanks. ........category.rb....... set_table_name "category" has_many :books, :dependent => :delete_all has_many :titles, :through => books, :dependent => :delete_all has_many :prices, :through => books, :dependent => :delete_all ........................... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Julian Leviston
2008-Apr-10 02:16 UTC
Re: Problems with copying and deleting with table relations
Please post all your models, and explain your problem. I could not find your original post. Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 10/04/2008, at 11:57 AM, user splash wrote:> > AndyV wrote: >> In the data model of your original post, title and price belong to >> book but have no relation defined for category. has that changed? >> Where is category_id being set for them? >> >> On Apr 7, 10:51 pm, user splash <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > > Hi, > > I have made some changes to the category model as shown below. The > other > models are still the same as posted in the original post. Is there > other > steps I''ve missed out? I my item and price tables, there are two > foreign > keys, category_id and book_id. Is there a way to solve the problems of > my third post? > > Please help. > > Thanks. > > > > ........category.rb....... > > set_table_name "category" > has_many :books, :dependent => :delete_all > has_many :titles, :through => books, :dependent => :delete_all > has_many :prices, :through => books, :dependent => :delete_all > ........................... > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-10 05:25 UTC
Re: Problems with copying and deleting with table relations
Julian Leviston wrote:> Please post all your models, and explain your problem. I could not > find your original post.After some changes, I am able to delete to copy the category using the following. But there is problem. The category_id, a foreign key in both the title and the price table, is showing the wrong id. It is getting category_id "1", id of the original copy of category instead of category_id "2", the id of the newly copied version of the original category. Example of the error (the following are ids that are in the various tables): Original Category Copy of original Category id => 1 id => 2 Book(belongs to original category) Book(belongs to copied category) id => 1 id => 2 category_id => 1 category_id => 2 Item Item book_id => 1 book_id => 2 category_id => 1 category_id => 1 (this category_id suppose to be 2) Price Price book_id => 1 book_id => 2 category_id => 1 category_id => 1 (this category_id suppose to be 2) I really need help. Can someone please help me with this? Thanks ........category model........ . . . . def deep_clone result = self.clone self.books.each { |b| result.books << b.deep_clone} result end .......book model............. . . . def deep_clone result = self.clone self.titles.each { |t| result.titles << t.clone} self.prices.each { |p| result.prices << p.clone} result end ......category controller........ def copy_all @category = Category.find(params[:id],:include => [:books, :titles, :prices]) @new_category = @category.deep_clone if @new_category.save redirect_to :action => "edit_copied" flash[:notice] = "Category copied! " else render :action => "index" flash[:notice] = "Category was not copied! " end end ......book controller.......... def copy_all @book = Book.find(params[:id],:include => [:titles, :prices]) @new_book = @book.deep_clone if @new_book.save redirect_to :action => "edit_copied_book" flash[:notice] = "Book copied! " else render :action => "index" flash[:notice] = "Book was not copied! " end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Julian Leviston
2008-Apr-10 06:22 UTC
Re: Problems with copying and deleting with table relations
Oh, okay now we have the models it makes a bit more sense. I think you probably need to save your records as you clone, don''t you? Also, ensure that ALL your keys are being saved properly as you clone them. Julian. Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) VIDEO #3 out NOW! http://sensei.zenunit.com/ On 10/04/2008, at 3:25 PM, user splash wrote:> > Julian Leviston wrote: >> Please post all your models, and explain your problem. I could not >> find your original post. > > After some changes, I am able to delete to copy the category using the > following. But there is problem. The category_id, a foreign key in > both > the title and the price table, is showing the wrong id. It is getting > category_id "1", id of the original copy of category instead of > category_id "2", the id of the newly copied version of the original > category. > > Example of the error (the following are ids that are in the various > tables): > > Original Category Copy of original Category > id => 1 id => 2 > > Book(belongs to original category) Book(belongs to copied > category) > id => 1 id => 2 > category_id => 1 category_id => 2 > > Item Item > book_id => 1 book_id => 2 > category_id => 1 category_id => 1 > (this category_id suppose to > be 2) > > Price Price > book_id => 1 book_id => 2 > category_id => 1 category_id => 1 > (this category_id suppose to > be 2) > > > > I really need help. Can someone please help me with this? > > Thanks > > > ........category model........ > . > . > . > . > def deep_clone > > result = self.clone > > self.books.each { |b| result.books << b.deep_clone} > > result > > end > > > .......book model............. > . > . > . > def deep_clone > > result = self.clone > > self.titles.each { |t| result.titles << t.clone} > > self.prices.each { |p| result.prices << p.clone} > > result > > end > > ......category controller........ > > def copy_all > @category = Category.find(params[:id],:include => [:books, :titles, > :prices]) > > @new_category = @category.deep_clone > > > > > if @new_category.save > redirect_to :action => "edit_copied" > flash[:notice] = "Category copied! " > > else > > render :action => "index" > > flash[:notice] = "Category was not copied! " > > end > end > > ......book controller.......... > > def copy_all > > @book = Book.find(params[:id],:include => [:titles, :prices]) > > @new_book = @book.deep_clone > > > if @new_book.save > redirect_to :action => "edit_copied_book" > flash[:notice] = "Book copied! " > else > render :action => "index" > flash[:notice] = "Book was not copied! " > end > end > > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-10 07:03 UTC
Re: Problems with copying and deleting with table relations
Julian Leviston wrote:> Oh, okay now we have the models it makes a bit more sense. > > I think you probably need to save your records as you clone, don''t you? > > Also, ensure that ALL your keys are being saved properly as you clone > them. > > Julian. > > Learn Ruby on Rails! Check out the FREE VIDS (for a limited time) > VIDEO #3 out NOW! > http://sensei.zenunit.com/Hi, How do I check if all the keys are being saved as I clone and all the keys are properly saved? I have also tried to clone the category and the save the category in the category controller. I have also changed result = self.clone to result = self.clone.save in the book model. But it doesn''t work. Please help. Thanks. .......book model............. . . . def deep_clone result = self.clone.save self.titles.each { |t| result.titles << t.clone} self.prices.each { |p| result.prices << p.clone} result end ......book controller.......... def copy_all @book = Book.find(params[:id],:include => [:titles, :prices]) @new_book = @book.deep_clone if @new_book.save redirect_to :action => "edit_copied_book" flash[:notice] = "Book copied! " else render :action => "index" flash[:notice] = "Book was not copied! " end end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
user splash
2008-Apr-14 01:24 UTC
Re: Problems with copying and deleting with table relations
> self.titles.each { |t| result.titles << t.clone} > > self.prices.each { |p| result.prices << p.clone} > > result > > end > > > ......book controller.......... > > def copy_all > > @book = Book.find(params[:id],:include => [:titles, :prices]) > > @new_book = @book.deep_clone > > > if @new_book.save > redirect_to :action => "edit_copied_book" > flash[:notice] = "Book copied! " > else > render :action => "index" > flash[:notice] = "Book was not copied! " > end > endHi, I still can''t solve this problem. Have been stuck for days. Please help. Thanks -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---