# Moving multiple attributes to a new table - please help!

**URL:** <https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761>\
**Category:** Rails\
**Created:** [April 25, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761 "2007-04-25T16:13:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [April 25, 2007, 4:13pm UTC](https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761/1 "2007-04-25T16:13:37Z")

</div>

Okay I have a “list” view that contains a package inventory stored in a  
table called addpackages. Attributes include fname, lname,  
packagetype, email, phone, etc.

Here’s what I’ve done already:

1. When you click the last name of a person in the list view simulating  
they are picking the package up, you go to my custom edit view.

 \<%= link\_to addpackage.lname, :action =\> "edit", :id =\> addpackage.id %\>
1. Here is my custom edit view:

**User must enter initials to claim package:**

1. This all works fine for getting the signature in the addpackages  
table. What I would like to do at this point is copy a few  
attributes(lname, fname, signature, phone) to a table called receipts?  
I’ve already created a relationship between addpackages and receipts, I  
just need help with coding behind this operation. I figure I would  
perform this operation in the addpackage controller under “def edit” but  
I really need a kick start to coding this or someone to tell me this is  
the correct way to do it.

Help will be greatly greatly appreciated!!!

-Tony

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [April 25, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761/2 "2007-04-25T17:38:26Z")

</div>

Tony M. wrote:

> What I would like to do at this point is copy a few  
> attributes(lname, fname, signature, phone) to a table called receipts?  
> I’ve already created a relationship between addpackages and receipts, I  
> just need help with coding behind this operation. I figure I would  
> perform this operation in the addpackage controller under “def edit” but  
> I really need a kick start to coding this or someone to tell me this is  
> the correct way to do it.

It would really help if you could give us the relationships between the  
models. From my understanding, you are trying to copy the fields from  
one table to another right? In that case, you can look up the package  
record in the receipt model and copy the attributes to the current  
records.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [April 25, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761/3 "2007-04-25T20:03:36Z")

</div>

Wai T. wrote:

> Tony M. wrote:
> 
> > What I would like to do at this point is copy a few  
> > attributes(lname, fname, signature, phone) to a table called receipts?  
> > I’ve already created a relationship between addpackages and receipts, I  
> > just need help with coding behind this operation. I figure I would  
> > perform this operation in the addpackage controller under “def edit” but  
> > I really need a kick start to coding this or someone to tell me this is  
> > the correct way to do it.
> 
> It would really help if you could give us the relationships between the  
> models. From my understanding, you are trying to copy the fields from  
> one table to another right? In that case, you can look up the package  
> record in the receipt model and copy the attributes to the current  
> records.

I think we are on the same page, but here’s a visual representation just  
in case.

1: The ID# is clicked when a user comes to the mailroom to get their  
package  
[http://aycu34.webshots.com/image/15313/2002794410554314194\_rs.jpg](http://aycu34.webshots.com/image/15313/2002794410554314194_rs.jpg)

2: The app will prompt for the users initials  
[http://aycu24.webshots.com/image/15343/2002700669700389807\_rs.jpg](http://aycu24.webshots.com/image/15343/2002700669700389807_rs.jpg)

3: Upon clicking the Sign for package, the initials/date are logged, and  
what I want to happen is to copy all all fields needed to fill this  
following Receipts table, and at the same time delete the selected  
record from Inventory, as the package has been picked up and it is no  
longer in the mailroom.  
[http://aycu01.webshots.com/image/13400/2002736555382712550\_rs.jpg](http://aycu01.webshots.com/image/13400/2002736555382712550_rs.jpg)

Here are my relations at the moment:

class Addpackage \< ActiveRecord::Base  
belongs\_to :category  
has\_and\_belongs\_to\_many :rosters  
has\_many :receipts  
end

class Receipt \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :addpackages  
end

I’ve been trying to do this operation from both the addpackage  
controller and receipt controller but have failed. I’m still a newbie  
to rails so it might just be my syntax or incomplete knowledge. You  
said above I could perform all these operations from receipt.db model?  
I have never put anything but relations in those.

Anyway I have exhausted myself trying to get this to work. I’ll keep  
hacking at it, but im not getting much of anywhere.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [April 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761/4 "2007-04-25T23:09:47Z")

</div>

Sorry to be such a bother but this is the only thing keeping me from  
completing my little project and im going bonkers lol. If anyone can  
throw me a bone now I will return the favor later.

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [April 25, 2007, 10:06pm UTC](https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761/5 "2007-04-25T22:06:39Z")

</div>

2 hours later I still can’t figure out how to make this work. Are my  
relations correct? If someone could throw me a couple lines of code  
or even point me in the right direction it would really make my day!!

-

---

<div class="post-metadata">

**Author:** ![merc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f07891/32.png) [@merc](https://www.ruby-forum.com/u/merc)\
**Post date:** [April 26, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/moving-multiple-attributes-to-a-new-table-please-help/102761/6 "2007-04-26T21:22:25Z")

</div>

The very words ‘copy attributes’ should send shivers down your spine  
as a Rails developer. It really sounds like what you’ve got is a  
third object that is related to packages and receipts – user/person/  
addressee or something of that sort. I’d think something like:

class User \< ActiveRecord::Base  
has\_many :packages  
has\_many :receipts  
…  
end  
(the class above carries the data you want to copy – name fields,  
etc)

class AddPackage \< ActiveRecord::Base  
belongs\_to :user  
…  
end

class Receipt \< ActiveRecord::Base  
belongs\_to :user  
…  
end

Your User class could have a method that “transfers” the item from  
“Inventory” (I don’t see any class relationship for that) to Receipts:

def signed\_for(add\_package)  
self.receipts.create(:add\_package\_id =\>  
add\_package.id, :picked\_up\_at =\> Time.now)

# some code to remove add\_package from inventory

end

Note: It’s MUCH better to put this type of business logic in the model  
and not the controller.

HTH,  
Andy

On Apr 25, 5:09 pm, “Tony M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
