# Problem appending an object to an array

**URL:** <https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317>\
**Category:** Rails\
**Created:** [November 18, 2005, 8:26pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317 "2005-11-18T20:26:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 18, 2005, 8:26pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/1 "2005-11-18T20:26:46Z")

</div>

Hello.

Here are the two offending lines of code: They are designed to  
provide a drop down list (working).

@bank =Org.find(:all, :conditions=\>“org\_type=‘bank’”)

@bank.unshift(Org.new  
(“name”=\>“none”,“id”=\>1000,“addr1”=\>“a”,“addr2”=\>“b”,“city”=\>“c”,“provin  
ce”=\>“d”,“post\_code”=\>“x”,

“country”=\>“Canada”,“gst\_number”=\>“y”,“logo”=\>“null”,“parent”=\>0,“inv\_pr  
efix”=\>“z”,“org\_type”=\>“bank”))

The problem is that when I inspect the @bank array I find that it  
does not have an “id” field. This is a problem because it means I  
cannot save the invoice record.

Any ideas?

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 18, 2005, 9:30pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/2 "2005-11-18T21:30:10Z")

</div>

On 11/18/05, Bruce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

I had a similar problem last night. Could it be that the ID is  
supposed to be assigned by the DB on create? That was my guess.

Dave

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 18, 2005, 9:36pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/3 "2005-11-18T21:36:13Z")

</div>

David C. wrote:

> > (“name”=\>“none”,“id”=\>1000,“addr1”=\>“a”,“addr2”=\>“b”,“city”=\>“c”,"provin
> > 
> > bruce
> 
> I had a similar problem last night. Could it be that the ID is  
> supposed to be assigned by the DB on create? That was my guess.

The Model.new and the Model#attributes= methods ignore any setting of  
the ID to  
prevent malicious attacks from the client to populate @params and set  
the id.

You have to set it separately, but it’s best to let the DB assign it.

Regards,  
Blair

–  
Blair Z., Ph.D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subversion and Orca training and consulting  
[http://www.orcaware.com/svn/](http://www.orcaware.com/svn/)

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 18, 2005, 10:33pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/4 "2005-11-18T22:33:32Z")

</div>

Hello.

I have to create a PDF from ruby (partly because browsers are so poor  
at printing html pages - why is that?). I am looking at  
PDF::Writer. Does anyone have any suggestions as to a better package  
than that - if there is one?

tia

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 18, 2005, 10:30pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/5 "2005-11-18T22:30:31Z")

</div>

David:

Thanks. I guessed the same but that was helping. So I “cheated”.

I solved it this way (I’d still like to know what the problem was)

if @invoice.bank\_id = “null”  
@invoince.bank\_id = 0

Which works for my purpose. These two lines of code come just before  
the Invoice.save method.

It works, but I feel kind of dirty. I feel there is something here  
that I have not understood that I should have understood.

I hope that this workaround / kludge / abuse of ruby purity may be of  
assistance, if you can’t find a cleaner method.

bruce

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 18, 2005, 10:39pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/6 "2005-11-18T22:39:35Z")

</div>

try using Org.create instead of Org.new (and remove the id attribute of  
course). create will attempt to save and return the new record. if the  
object could not be saved, then the unsaved object is returned.

@bank.unshift(Org.new(“name”=\>“none”,  
“addr1”=\>“a”,  
“addr2”=\>“b”,  
“city”=\>“c”,  
“province”=\>“d”,  
“post\_code”=\>“x”,  
“country”=\>“Canada”,  
“gst\_number”=\>“y”,  
“logo”=\>“null”,  
“parent”=\>0,  
“inv\_prefix”=\>“z”,  
“org\_type”=\>“bank”))

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 19, 2005, 6:57am UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/7 "2005-11-19T06:57:32Z")

</div>

On 11/18/05, Bruce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello.
> 
> I have to create a PDF from ruby (partly because browsers are so poor  
> at printing html pages - why is that?). I am looking at  
> PDF::Writer. Does anyone have any suggestions as to a better package  
> than that - if there is one?

I did quite a bit of research and found PDF::Writer to be the best  
solution for ruby. Just got done using it on a project for a client  
and it’s worked just fine.

Chris

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 18, 2005, 11:49pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/8 "2005-11-18T23:49:17Z")

</div>

On 11/18/05, Bruce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have to create a PDF from ruby (partly because browsers are so poor  
> at printing html pages - why is that?). I am looking at  
> PDF::Writer. Does anyone have any suggestions as to a better package  
> than that - if there is one?

There are multiple packages available, however – and I will be  
completely unbiased here 😉 – PDF::Writer is the best option you’ll  
find for Ruby right now. If, however, you end up not liking the  
PDF::Writer API, I can recommend that the Ruby implementation of FPDF  
produces nice looking documents, even though I can’t stand the API.

PDF::Writer will be taking some ideas from the original PHP  
implementation of FPDF for future releases, but the overall  
capabilities of PDF::Writer are greater than those offered by FPDF.

## -austin

Austin Z. \* removed\_email\_address@domain.invalid  
\* Alternate: removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![brucebalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ec8ea/32.png) [@brucebalmer](https://www.ruby-forum.com/u/brucebalmer)\
**Post date:** [November 20, 2005, 12:11pm UTC](https://www.ruby-forum.com/t/problem-appending-an-object-to-an-array/48317/9 "2005-11-20T12:11:23Z")

</div>

±le 18/11/2005 17:16 -0500, Austin Z. écrivait :  
| On 11/18/05, Bruce B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
|\> I have to create a PDF from ruby (partly because browsers are so poor  
|\> at printing html pages - why is that?). I am looking at  
|\> PDF::Writer. Does anyone have any suggestions as to a better package  
|\> than that - if there is one?  
|  
| There are multiple packages available, however – and I will be  
| completely unbiased here 😉 – PDF::Writer is the best option you’ll  
| find for Ruby right now. If, however, you end up not liking the  
| PDF::Writer API, I can recommend that the Ruby implementation of FPDF  
| produces nice looking documents, even though I can’t stand the API.

Plus, using the template handler near the middle of  
[http://wiki.rubyonrails.com/rails/pages/HowtoGeneratePDFs](http://wiki.rubyonrails.com/rails/pages/HowtoGeneratePDFs)

it’s really the nicest thing you can do 🙂
