# Adding extra value to an object

**URL:** <https://www.ruby-forum.com/t/adding-extra-value-to-an-object/69056>\
**Category:** Rails\
**Created:** [December 16, 2005, 8:33am UTC](https://www.ruby-forum.com/t/adding-extra-value-to-an-object/69056 "2005-12-16T08:33:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![naroor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@naroor](https://www.ruby-forum.com/u/naroor)\
**Post date:** [December 16, 2005, 8:33am UTC](https://www.ruby-forum.com/t/adding-extra-value-to-an-object/69056/1 "2005-12-16T08:33:32Z")

</div>

Hello friends ,  
I have a clear method here ::

def create  
@invoiceitems = Invoiceitems.new(params[:invoiceitems])  
@invoiceitems[“invoice\_id”] = params[:id]  
if @invoiceitems.save  
flash[:notice] = ‘Invoiceitems was successfully created.’  
redirect\_to :action =\> ‘new’,:id =\> params[:id]  
else  
render :action =\> ‘new’  
end  
end

In the table invoiceitems I have a field called invoice\_id which is a  
foreign key and the primary key of the table invoices. I need to save  
the invoiceid which is in the URL itself (params[:id]) before saving  
.How can I do it .  
The above method is not storing that .

Thanx and regards,  
Naroor R.,  
[www.naroor.blogspot.com](http://www.naroor.blogspot.com)

---

<div class="post-metadata">

**Author:** ![naroor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@naroor](https://www.ruby-forum.com/u/naroor)\
**Post date:** [December 16, 2005, 10:07am UTC](https://www.ruby-forum.com/t/adding-extra-value-to-an-object/69056/2 "2005-12-16T10:07:09Z")

</div>

Naroor, how did you set up the association in your Invoiceitems model?  
I suspect either the association is not set up right, or the :id  
parameter is not coming in with the request. A functional test for  
this action would be a good idea. (goes for all your other actions as  
well 🙂

As an aside, it will benefit you in the long to be consistent with  
plural/singular in naming your identifiers. Consider calling your  
class Invoiceitem as opposed to Invoiceitems, same for the instance  
variable.

cheers  
Gerret

---

<div class="post-metadata">

**Author:** ![naroor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@naroor](https://www.ruby-forum.com/u/naroor)\
**Post date:** [December 16, 2005, 10:15am UTC](https://www.ruby-forum.com/t/adding-extra-value-to-an-object/69056/3 "2005-12-16T10:15:21Z")

</div>

Hi Gerret,

class Invoice \< ActiveRecord::Base  
belongs\_to :company  
has\_many :invoiceitems  
end

class Invoiceitems \< ActiveRecord::Base  
belongs\_to :invoice  
end

These are my model entries .Please let me know what wrong here ?

Thanx and regards,  
Naroor R.

---

<div class="post-metadata">

**Author:** ![naroor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@naroor](https://www.ruby-forum.com/u/naroor)\
**Post date:** [December 16, 2005, 10:22am UTC](https://www.ruby-forum.com/t/adding-extra-value-to-an-object/69056/4 "2005-12-16T10:22:54Z")

</div>

Gerret ,  
Another try that I made was to create a hidden text box and add the  
params[:id] to that and save the whole contents .There also it didnt  
work .

Please comment .  
Thanx and regards ,  
Naroor R.

---

<div class="post-metadata">

**Author:** ![naroor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d26b3c/32.png) [@naroor](https://www.ruby-forum.com/u/naroor)\
**Post date:** [December 16, 2005, 11:28am UTC](https://www.ruby-forum.com/t/adding-extra-value-to-an-object/69056/5 "2005-12-16T11:28:25Z")

</div>

Naroor,

your model files are correct.

I suggest we clean up the names of your identifies before looking  
further at the problem. I think this is where part of the problem  
stems from. AFAICT your create method intends to save a single  
Invoiceitem, attach it to an Invoice, and then redirect to a screen  
where another Invoiceitem may be added.

So I suggest two cleanup steps:

1. Rename your Invoicesitems class to Invoiceitem. Singular instead of  
Plural. That’s the rails convention, and if you follow it, your code  
will read much more easily. Same goes for your instance variable  
@invoiceitems. Make it @invoiceitem instead.  
I suspect you used “./script/generate invoiceitems” to create your  
controller. You should pass a singular parameter, so re-run  
./script/generate like “./script/generate invoiceitem”. That will give  
you a class Invoiceitem instead of Invoiceitems (your table must still  
be called ‘invoiceitems’, plural. Which makes sense, because it  
contains many items).

2. In your create method, you are using the parameter :id is the  
invoice id. Don’t. In your form, have a parameter :invoice\_id, and  
don’t use the :id parameter. This makes it very clear which class your  
id-parameter belongs to. With your current code, you implicitly  
initialize @invoiceitems.id to params[:id], which is not what you  
want.

With those two changes done your method will look like this:

def create  
@invoiceitem = Invoiceitem.new(params[:invoiceitem])  
if @invoiceitem.save  
flash[:notice] = ‘Invoiceitem was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

Because we’ve stuck to naming conventions, the line

@invoiceitems[“invoice\_id”] = params[:invoiceitem][:invoice\_id]

become superfluous, because code to that effect is automatically  
performed by Invoiceitem.new(params[:invoiceitem]).

Of course you need to supply a parameter [:invoiceitem][:incoide\_id].  
In your new.rhtml, you might do this:

\<%= hidden\_field ‘invoiceitem’, ‘incoice\_id’, :value =\> @invoice.id %\>

Good luck, hope this helps. Use functional tests for debugging, its a  
real timesave once you’re used to them.

cheers  
Gerret
