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 %>
…(book’s view)index.rhtml…
<% for book in @books %>