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.
brucebalmer (Guest)
on 2005-11-18 21: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
digidigo (Guest)
on 2005-11-18 22:30
(Received via mailing list)
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
blair (Guest)
on 2005-11-18 22:36
(Received via mailing list)
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/
brucebalmer (Guest)
on 2005-11-18 23: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
brucebalmer (Guest)
on 2005-11-18 23: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
christopher.k.hall (Guest)
on 2005-11-18 23: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"))
halostatue (Guest)
on 2005-11-19 00:49
(Received via mailing list)
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 ;) -- 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
snacktime (Guest)
on 2005-11-19 07:57
(Received via mailing list)
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
mat (Guest)
on 2005-11-20 13:11
(Received via mailing list)
+-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 ;) -- 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.