Moving multiple attributes to a new table - please help!

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

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.

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

2: The app will prompt for the users initials
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

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.

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.

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

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