Problem appending an object to an array


#1

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


#2

On 11/18/05, Bruce B. 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


#3

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
Subversion and Orca training and consulting
http://www.orcaware.com/svn/


#4

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


#5

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


#6

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”))


#7

On 11/18/05, Bruce B. 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


#8

On 11/18/05, Bruce B. 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 :wink: – 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


#9

±le 18/11/2005 17:16 -0500, Austin Z. écrivait :
| On 11/18/05, Bruce B. 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 :wink: – 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

it’s really the nicest thing you can do :slight_smile: