Forum: Ruby on Rails Problem appending an object to an array.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 brucebalmer (Guest)
on 2005-11-18 20:26
(Received via mailing list)
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
Bd6167863690cb320d65bb9204619817?d=identicon&s=25 digidigo (Guest)
on 2005-11-18 21:30
(Received via mailing list)
On 11/18/05, Bruce Balmer <brucebalmer@mac.com> 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
27c57aaa4bda5ac8b0593659573b522f?d=identicon&s=25 blair (Guest)
on 2005-11-18 21:36
(Received via mailing list)
David Clements 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 Zajac, Ph.D.
<blair@orcaware.com>
Subversion and Orca training and consulting
http://www.orcaware.com/svn/
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 brucebalmer (Guest)
on 2005-11-18 22:30
(Received via mailing list)
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
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 brucebalmer (Guest)
on 2005-11-18 22:33
(Received via mailing list)
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
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 christopher.k.hall (Guest)
on 2005-11-18 22:39
(Received via mailing list)
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"))
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 halostatue (Guest)
on 2005-11-18 23:49
(Received via mailing list)
On 11/18/05, Bruce Balmer <brucebalmer@mac.com> 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 Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca
F48118fe74b0c7f6fd82a0ee422fa34e?d=identicon&s=25 snacktime (Guest)
on 2005-11-19 06:57
(Received via mailing list)
On 11/18/05, Bruce Balmer <brucebalmer@mac.com> 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
2ad1c05e8fa25d4ca9dc218fa33d2d14?d=identicon&s=25 mat (Guest)
on 2005-11-20 12:11
(Received via mailing list)
+-le 18/11/2005 17:16 -0500, Austin Ziegler écrivait :
| On 11/18/05, Bruce Balmer <brucebalmer@mac.com> 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

it's really the nicest thing you can do :-)
This topic is locked and can not be replied to.