Problems with copying and deleting with table relations

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 %>

<%=h category.name %> <%= link_to 'Show', :action => "show", :id => category.id %> <%= link_to 'Edit', :action => "edit", :id => category.id %> <%= link_to 'Copy', :action => "copy_all", :id => category.id %> <%= link_to 'Destroy', { :action => "delete", :id => category.id }, {:post => true, :confirm => 'Are you sure you want to delete?'} %> <% end %>

…(book’s view)index.rhtml…

<% for book in @books %>

<%=h book.name %> <%= link_to 'Show', :action => "show", :id => book.id %> <%= link_to 'Edit', :action => "edit", :id => book.id %> <%= link_to 'Copy', :action => "copy_all", :id => book.id %> <%= link_to 'Destroy', { :action => "delete", :id => book.id }, {:post => true, :confirm => 'Are you sure you want to delete?'} %> <% end %>

user splash wrote:

…(book’s view)index.rhtml…

<% for book in @books %>

<%=h book.name %> <%= link_to 'Show', :action => "show", :id => book.id %> <%= link_to 'Edit', :action => "edit", :id => book.id %> <%= link_to 'Copy', :action => "copy_all", :id => book.id %> <%= link_to 'Destroy', { :action => "delete", :id => book.id }, {:post => true, :confirm => 'Are you sure you want to delete?'} %> <% 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.

user splash wrote:

user splash wrote:

…(book’s view)index.rhtml…

<% for book in @books %>

<%=h book.name %> <%= link_to 'Show', :action => "show", :id => book.id %> <%= link_to 'Edit', :action => "edit", :id => book.id %> <%= 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

user splash wrote:

…(book’s view)index.rhtml…

action => “edit”, :id => book.id %>

<%= 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.

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 [email protected]

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 [email protected]

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

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/

Julian L. 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

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/

Julian L. 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

 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

Hi,

I still can’t solve this problem. Have been stuck for days. Please help.

Thanks

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs