Is it better to come upon a flower and to believe it was created for me, or to see the flower, know its blossom, and rejoice for I was there to see it.
Page 33 of 51
I: Jesus, my man, what do you have for us today?
J: I’m glad to see you’re loosening up a bit. I’m just here during Holy Week to throw out a little bone for those of you into the whole worship thing. While you’re all running around preparing for Good Friday and Easter, I’d like to reprise my last outing here.
I: The cookie one?
J: Yeah, the cookie one. It seems that a few of you business types needed a change of vocabulary to get the cookie theme. So I’m gonna hit from a different angle.
Let pretend that cookies are capital. I’m talking in economic terms now. Cookies are capital. I give you a cookie. What are you going to do with it?
I: Um, eat it or share it?
J: Yes, but more to the point, you’re going to use the cookie for something. The purpose of a cookie is easy to divine. You are going to use the cookie for some purpose for which it was intended. If it was a gift, you will say thank you and probably enjoy it. You probably wouldn’t reply, ‘What the hell are you giving me this cookie for? I didn’t ask for it. What am I supposed to do with it?’ That’s just silly, right?
I: I guess. That would be pretty stupid. I mean, cookies are tasty.
J: Exactly what I’m saying! Now what if you’re on a diet? Do you have the right to get upset if I give you a cookie?
I: No, I think good manners would dictate that you would find something to do with the cookie if you weren’t going to eat it.
J: If you are a thoughtful person, you would show good manners. A gift is a gift. You’re just not allowed to complain about gifts.
So anyway, I hand you some capital. I give you some money. What are you going to do with it? Bury it? Hide it? Preserve it in some way? Make it last as long as possible? If you know anything about money, you know it’s more valuable right now than it is in the future. That’s why people pay interest rates to get it right now. An interest rate represents the present value of future money.
People with a purpose for the dough will pay through the nose to get it right now so they can put it to good use and hopefully earn more than what they paid for it.
Now, say I just hand it to you. I give you a non-taxable lump sum on the order of a couple million smackers. What do you do with it?
I: Gosh, that’s such an improbable event, I’d not thought about it.
J: Not many people have, but I’ll tell you what; they should. Capital is like your life. If you don’t know what you’d do with it immediately, then you don’t know what you’re doing. If you don’t know what you’re doing, then you’d better drop EVERYTHING and figure it out pretty damn quick. Your investors are getting antsy ’cause you’re wasting the capital. You’re wasting the cookie. It’s getting moldy, and your capital is losing value to inflation.
Your life is a big pile of capital that needs to be used RIGHT NOW. It’s most valuable RIGHT NOW. It can only make a difference RIGHT NOW.
Notice a pattern?
I don’t think anyone would curse me for giving them a pile of capital. Why do you think they get upset that they have a life? I know life is hard sometimes. I really do know. Yet, to have it is a blessing. It is a grace bestowed for which you didn’t ask. You don’t deserve it. Whether you deserve the cookie, capital, or your life is irrelevant, totally and completely irrelevant. What is relevant is that you’ve got something that few have, that few have the opportunity to use. There is a whole lot of life in the universe just busting to come out and live. Not everyone has the opportunity you have right now.
So, Mr. Business Guy, what is it gonna be? Are you going to offend your biggest investor, ME? Do you want put the capital to good use, or are you going to sit on it and fret. Capital is not to be preserved, just as cookies are not to be kept under glass, and your life not lived in quiet seclusion far from danger.
Now get busy, Time is Money. I don’t want to have to fire you. *wink*
The whole mess of fraudulent fines is blowing up here now. The local paper is featuring the story and outrage on the front page. Through the article, I learned that I can go online and check my driver’s record to see just what fines I supposedly owe. Okay, cool, I’ll at least be able to see what they have fabricated for my fine-paying pleasure.
THEY DON’T EVEN SHOW THE SAME AMOUNT.
Let me repeat that. The hard copy printed letter on paper, that had to come from somewhere, that had to be generated from something by someone, doesn’t even match what they have in their own database – not even close.
I knew criminals were stupid.
Supposedly, I was driving through red lights hours away in Mayaguez a week after 9/11. DTOP shows a fine for $30 and no license plate number. So apparently I was jogging through red lights on the other side of the Puerto Rico with my special jogging sneakers and super powers. Luckily I had my license so that they could indicate the proper fine for flying through red lights with an invisible car or something.
BAH! So where does the $120 come from? I think they just made it up, pulled a number from their collective ass, and called out –
Hah, schenanegans on me. You kidders you.
In orbit around the Earth, they were safe, safe and isolated from the depths of space by their craft, their suits, their technology. They were safe from the vacuum, the cold, the radiation, and small chunks of debris. They were as safe and comfortable as in their kitchen sipping tea and reading the Times. "Martha, will you fetch me some toast? Thanks, you’re an angel." Thanks to the wonderful technology of their deep space craft and its marvelous systems, designed by the finest minds of 22st century Earth and swaddled as they were in their cradles of poly-alloy something, they had not a care in the world. Not a one.
"What was that?" Justin breathed into his helmet microphone. "I think I heard something."
A voice responded. It was helm control. "I dunno," he whispered, as if asleep, "I think we’re approaching the outer atmosphere. Sometimes the heat makes things creak." At least it sounded like creak. It could have been creep, or weak. Justin couldn’t tell.
"Um, okay." It wasn’t important, he guessed. The helmsman was a stout sort of fellow, predictable and faithful. He always showed up on time, checked the craft, before launch. He was a by-the-book sort not prone to imaginative thinking, but he did his job, which was good enough surely, and probably what you want in a helmsman.
Justin looked around at the relaxed forms of the other passengers. They were scientists, like himself, but perhaps not like himself. They were fascinated by things other than a little re-entry. They obsessed over big problems or small problems, tiny little worlds or grand grandiose big big worlds. Make the little worlds bigger, they’d say. Make the big worlds smaller, would reply the others – two schools of thought, Justin reflected, two schools of thought that always end up in the same place.
Justin was awake now, and he couldn’t close his eyes. The Earth was this big beautiful ball of blue, crystalline blue, shiny, reflective, shimmery, but calm, peaceful, enveloping. It’s like you could just reach out and touch it, squeeze it, wrap it all around you, he thought, just roll around on it. Man, he thought adjusting his poly-alloy something pants, been out here too long – getting turned on by this big blue ball in space. Geez.
"Hey, Melinda," he whispered though the microphone, "did you get the data you were looking for?"
"Hmmm… you talkin’ to me, Justin? Yeah, yeah, I got what I was looking for. Gracias a Dios. They were there just waiting for me. I stepped around the corner and there they were as if they had chosen me. The mission was un exito total."
"I’m glad." He had had no such luck. His first opportunity out here had netted him nothing, nothing, and now that he thought about it, nothing. Maybe when they got back, he’d see if maybe he could salvage at least something of this nothing of a trip. "I’m glad for you Melinda. Couldn’t have happened to a better person. You know you’re the best."
"Thanks, Justin. You’ll get something, soon, I’m sure."
There was that weak, creeping creak again, trickling over-head. "There it is again? Did you hear that? What the hell is that sound?"
The whisper came again, "Look, it’s nothing to be worried about, the hull’s heating up. It does that, uneven heating, causes uneven expansion, uneven compression. It’s all taken care of. Now, newbie, if you want to make yourself useful, lie back and close those big weepy eyes of yours. I’ll get you back to your mama’s arms before you wet yourself, I promise." And he clicked off his mic.
To Justin, the break-up seemed almost in slow motion. There was a shudder, and the pieces came off like big giant flakes of rust spinning out and away into the blackness their edges glowing faintly, discolored like the petals of a dying flower.
And down they fell.
…to be continued
I don’t want my desktop all candified. I don’t want media. I don’t want games. I don’t want music. I don’t want VOIP. I don’t want chat. I don’t want digg, reddit, and slashdot.
ARRRRRGGGGGGGHHHHHH!
I used to be productive. Now I have MAME to emulate all my favorite standup video games from the eighties and nineties (endless hours of fun). I now have instant messaging, every manner of music, video, and entertainment imaginable (not to mention all that P2P goodness and without the spyware). MythTV handles the PVR functions and let me tell you, it’s great.
It’s too great.
The problem is, I need to do some friggin’ work on this computer. I remember when the desktop was austere. I was forced to work, not goof off watching stupid video clips from video.google.com or www.youtube.com.
HEY YOU MEATHEADS stop messing with the desktop. It’s too good. It’s too distracting. Firefox is too good. There are too many great extensions. Mplayer is the best media player on the planet. Stop it, now!
I need to do some WORK!
Please make my desktop the non-functioning piece of utilitarian crap that it was five years ago, please please please?
I beg you.
I got a letter in the mail, addressed to me, from the DTOP (Departamento de Transportación y Obras Públicas, Dept of Transportation and Public Works). Weird, I thought, this is weird indeed. What could they possibly want with me? I quote (my thoughts in parenthesis)
Esteemed James, (ooo, I’m esteemed, that’s good, huh?)
I have good news for you (oh my goodness, good news, let me read more). You can save $48.00. (wow, how? more more more!) Currently, our system reflects that you have pending traffic fines totaling $120.00 (WHAT!?)
Take advantage of this offer. You have from March 3rd to May 1st to receive a 40% discount, a savings of $48.00. You can get out of all your fines by paying only $72.00. (WTF?)
This offer is for a limited time and will not be repeated. Don’t waste time or money. Visit your closest local collecting office and pay your invoice. You only have to bring this letter. (They wish)
Pay now and save. Don’t wait until it is tool late. (or WHAT?)
Gabriel D Alcaraz Emmanuelli (Crook)
Secretario de Transportación y Obras Públicas
Okay, it looks like it came from DTOP, on official stationary. They are not asking for a mailed check. They ask that you go to the official government collections office to pay. The letter is legitimately from DTOP, but it reads like a bad CompUSA rebate offer. It just sounds like fraud, fraud disguised as a great offer. Couple that with the fact that I just re-registered my car a week ago (so I was clear a week ago) and have never ever ever been pulled over let alone ticketed. This is just false. And the letter does not provide a phone number or any information to report an error. Okay, I think to myself, I’ll have to deal with this. What a pain.
I am reminded of a corollary to Arthur C. Clark’s "Any
sufficiently advanced technology is indistinguishable from magic" which
is "Any sufficiently advanced incompetence is indistinguishable from
malice." I love that quote.
Then I find out that Laura’s father, mother, and brother also got the same letter.
Okay, so that’s how it going to be, huh, naked malice. There is speculation that the cover story is going to be a computer glitch, that if you drag your sorry ass into the collections office, wait all day for a useless government functionary to see you, you may have your "fines" removed. Otherwise, if your time/job is worth more than the extortion demand, then you’ll just pay it and move on with your life.
I also find it weird that amount of the "fine" is suspiciously close to
the median daily income of the average worker. It’s like the dollar
figure is perfectly calculated to match the cost of missing a day of work. It’s probably a close race for the majority of Puerto Ricans
today. It’s just too perfect an amount for my "fines." It’s too
perfectly crafted. Too high, everybody fights, Government gets no
money. Too low, and most everybody pays it resulting in an increase in
revenue, but a loss on how much they could have actually gotten. The
tone of the letter makes me believe they are doing me a favor, going
out on a limb, and the amount is just about right for the average joe.
This is just sick, sick, sick. I have never heard of such a thing, government run extortion, fraud. I mean, usually they just stick to back room graft and mismanagement. How often do governments just come right out and say, "pay us this money, and there won’t be any problems," except in Cameroon?
Any lawyers out there want to take up a case against the Department of Transportation in Puerto Rico and Gabriel D Alcaraz Emmanuelli, alleged mastermind of this fraud?
By the time he was done, he had cake in his hair, ears, and down his back. He beamed, eyes twinkling, with true happiness as we all sang happy birthday to him. He knew we were adoring him, and the little munchkin ate it up, along with the cake.
It’s funny, Laura and I were reflecting on the night that Javier arrived. We were up late watching Battlestar Galatica (great great show) and about half way through, Laura starting having contractions. As with all pregnancies, you don’t know how much time you have, and this being our third child we could have easily had the little boy right there on the coffee table during the commercial break. Laura, however, sci-fi chick that she is, sucked it up and said that it’d be fine, that we could finish Battlestar Galactica.
"Are you sure?" I asked.
"Yeah, I guess so."
So little Javier, I just want to say on your 1st birthday, thanks for letting us watch our show. You’re the best. And Laura, you win the Sci-fi-chick-of-the year award.
Pardon me while I geek out again. This particular article falls under the, "look Ma, no hands" category, a shameless play for adoration of my cleverness. What I’m about to show is neither new nor terribly beautiful, nor cost effective for most professional flash designers and/or web designers.
It’s just cool that it was done procedurally and entirely on Linux. First, another caveat; I absolutely do not get these animation tools with their tweening, paths and visual interactions for creation of animations. For some reason, when I animate my stupid brain sees a stack of cells, straight up and down. I see the frame changes like a flip book. If I try to abstract an object in the animation into some sort of mathematical formula, line, path, or whatever, my little pile of gray matter goes all slushy. Also, my brain likes to see things in text. I know, I know, what’s the matter with me? I don’t even see the code. All I see is blonde, brunette, red-head. Chuckle.
Here’s the finished product: www.og-consulting.com
The door scene modeled in POV-ray scene description language and is typical of Old San Juan, Puerto Rico Spanish Colonial architecture. The real doors are beautiful. If you ever get a chance to take a cruise from/to Puerto Rico, don’t miss the chance to walk around Old San Juan (El Viejo San Juan) and check them out. I rendered three frames with the doors rotated from 0 to 75 degrees to simulate… guess what? Opening doors. Clever, huh?
There’s a lot of POV code, here’s a little bit of what it looks like:
cylinder { <0, 0.5, 0>, <0, -0.5, 0>, 0.5 texture { pigment { color rgb <0.6, 0, 0> } normal { granite 0.02 turbulence <0.5, 0.9, 0.2> scale 0.25 } finish { specular 0.1 reflection .1 } } }
It’s pretty simple. You place objects in an X-Y-Z space (Cartesian coordinates) like a sphere, cylinder, box or any other of the predefined primitives. You can merge them, subtract them, intersect them in creative ways. Finally, you apply some sort of texture which includes a pigment, a surface (normal), and a finish (reflections effects etc). There are easier ways to model, but sometimes POV-ray’s scene description language is just the most elegant and easiest way to model something.
The next step was to convert the png files to jpegs for inclusion in the flash animation. I used a little sprinkle of bash and a dash of kosher ImageMagick’s convert.
for file in *.png; do convert -quality 100 "$file" "${file%.png}.jpg"; done
For each png file, convert the png file to a 100 percent quality jpeg file. We use 100% quality because I’m going to let the swftools take care of the final compression. There’s no sense in lossy compressing then lossy compressing again. That’s just crazy talk.
We will now create the flash source file. Open a new file slideshow.sc. This is the textual language for the swftools Linux Flash toolkit. I have never ever ever looked at a flash source file from any Macromedia product, so I have no idea if this animation description method looks/acts/walks/talks in anyway shape or form like Macromedia’s products. Don’t know, don’t care.
.flash bbox=640x480 filename="slideshow.swf" version=6 fps=25 compress background=white .jpeg s1 "puerta_open03.jpg" .jpeg s2 "puerta_open02.jpg" .jpeg s3 "puerta_open01.jpg" .font font "gilc____.ttf" .font arial "arialbi.ttf" .text text1 text="Opening Doors" font=font .text text2 text="Opening Doors" font=font .text text3 text="Opening Doors" font=font .text text4 text="OG" font=arial .text text5 text="Experts in" font=font .text text6 text="Open Source" font=font .put s1 scalex=640 scaley=480 alpha=100% .put text1 scale=100% x=40 y=220 alpha=0% .frame 25 .put s2 scalex=640 scaley=480 alpha=0% .change text1 alpha=100% .put text2 scale=100% x=40 y=220 .frame 100 .change s2 alpha=0% .change text1 alpha=100% .change text2 alpha=100% .frame 125 .change text1 alpha=0% .change text2 alpha=100% .change s2 alpha=100% .put s3 scalex=640 scaley=480 alpha=0% .put text3 scale=100% x=40 y=220
And so on (that’s not the whole file, but the rest is just repetition. There are 71 total lines of code for that little flash animation. Is that a lot or a little? Seems pretty small to me anyway. Basically, with swftools you define an object (image, or text); you put it; then you change it. You can change its fade, size, location, and more. The swfc program will implement the change from the object’s last known state. Check out swftools for more examples (that’s where I got all the reference I needed to make my little flash thingie).
So there you have it. I walk through the frames, changing elements, putting, fading, growing, moving stuff around. It’s was all described from start to finish in a procedural language, from the POV-ray scene description language, to bash and ImageMagick for command line image manipulation, and finally to swftools flash scene language for the final animation. Pretty nifty, huh?
The neat thing about this is that once the procedure has been developed, you can reuse it for other clients, other looks, colors, messages, etc. In fact, you could directly render it on the server to update information on the fly via end user input. There’s no limit to what you can do with something like this.
Of course if you’re on Windows, you would probably just buy Macromedia’s software… but where’s the fun in that?
Last night I was at the juvenile processing facility. I got to meet a kid named Phillip. American name, but didn’t speak a word of English. I thought it weird. Whatever.
He was a baby, barely 15, but he wanted to be a boxer. He’d trained with his uncle before winding up in prison on a year and half sentence. I secretly wondered what a 15 year old could do to wind up in prison for 18 months. Geez. Either that or what I’ve heard about the racket of "lawyers" extorting money in the projects para bregar was actually true.
"You have a friend who gives you a Playstation… what do you do with it?"
Puzzled look – like I was trying to trip him up with a trick question, like if he said, play the damn thing, I’d zing him and say… no you give it to charity and spend more time in church on your knees thanking the good Lord for your life. Remember to wail and gnash your teeth. He loves that.
"You would… " I motioned with my thumbs as if to fiddle with the controller.
"Play it?"
"Yeah!" I exclaimed, "You play the thing. That’s why your friend gave it to you. And what is a Playstation for? To be P L A Y E D."
Phillip smiled a 15 year old smile, ear to ear, clean teeth to clean shining teeth.
"So now you’ve got this Playstation that you didn’t ask for. You kind of know what to do with it, but it’s really no fun by yourself. Who do you call first?"
"Mis amigos?" He offered.
"Right. Then you get together and share your gift with your buds. You pass time with them sharing the Playstation."
"Yeah." Phillip smiled again.
"Phillip, what’s your talent? What do you want to do with your life?"
Phillip hesitated. I don’t know why, I had not been trying to trick him. The quickest and most natural thought is usually the right one. I had not led him astray up to that point. He stilled seemed to be searching for some kind of noble Godly vision of what he should be doing with his life instead of what he wanted to do, what he was good at. He gathered the courage and offered:
"I want to be a boxer."
"Cool," And without missing a beat, "Did you know that Jesus was a boxer?"
Again Phillip was thrown for a loop. Jesus a boxer? How could it be? Boxing isn’t Godly. Boxing isn’t pious. Boxing is at best the red-light district of sports and humanity.
"Have you ever heard the phrase, ‘leave it all on the field’? Or in your case ‘leave it all in the ring’?"
"No." He looked at me quizzically.
"Well, after you fight, you should barely be alive. After you finish, you shouldn’t be able to stand. After you are through, you will have nothing more to give, no interviews, no congratulations, no celebrations, and if you lose no sorrows, no regrets, nothing. You will have used it all up and left it in the ring."
Phillip looked at me, eyes wide in puzzlement or amazement.
"Phillip, for what purpose do you have this life? Did you ask for it?"
"Eh?"
"Are you going to get out alive? Does anyone live forever?"
"No, I guess not."
"Isn’t it a gift like the Playstation? Aren’t all gifts just that; not asked for? And just like a gift that you didn’t ask for, the best thing you can do with it is use it, before it breaks, before it becomes obsolete… or the Playstation III comes along." Phillip chuckled. "What would you save it for anyway? You’ve got to use it.
Jesus wasn’t just a boxer? He was the champion of the world, uncontested, undefeated, even in death. He knew one thing that only the greatest champions have ever come close to knowing. He knew that how you do a thing is more important than anything else. That whatever you do, you live it fully, completely, with no regrets.
When you box, it’s a spiritual exercise. In order to do it well, you’ve got to study it. You have to train. You have to discipline yourself. You must have respect for it. With all the things you do you pay homage to your life and your life is that which has been given to you as a gift. You honor your gift by being the best boxer you can be.
Remember too, though, that all gifts carry a burden. You have a heavy responsibility. If you want to be like Miguel Cotto you have a heavy burden to carry. You might see his victories, his money, or his fame, but his bouts show his discipline, his patience, his devotion to his craft. He’s not hanging with his friends in the evenings. He’s training early in the morning and getting his rest. When he’s not training or resting, he’s probably reviewing films, studying his sport or eating a special diet. Yes, he has time for friends, but he’s a championship boxer and it’s not easy. And he’s surely not getting in trouble.
Phillip, is this future what you want? Do you accept this? Will you take up the burden, the responsibility, and the commitment to make your dream a reality?"
"Yes."
Yes, I understand this is going to be a weird post for some of you. Frankly, I haven’t been writing enough about what I spend most of my day doing, so here it is.
I have been having a bafflingly hard time trying to figure out the proper way to insert multiple child records from one single webform. It is the standard fare for posting things like invoice headers and details. Say for example, you’ve got an invoice record which consists of an order number, date and whatnot. That particular piece of tabular data is then considered the parent of its child line items (product_id, description, quantity, price, etc). So you’ve got this Order which consists of an invoice and line items.
It’s pretty basic, but I’ve had the hardest time figuring out how to do this is Ruby on Rails. I know it’s not hard in theory, but with Rails, since there is only One Right Way(TM) to do things, ’cause you’re on Rails, it takes a bit of doing to figure out this Right Way(TM). I personally don’t have a problem doing it Rails’ way, but please dear God, just tell me what it is. I bought the book and everything.
So here’s the dope, folks. Please correct me if there’s a better way of doing this, ’cause I’m a Rails n00b. For reference sake, I am using Ruby on Rails 1.0 with Postgres 8.0.4 and Ruby 1.8.4
First the webform:
<ul id=items>
<% for item in @items %>
<li><%= check_box_tag 'line_item', item.id, checked=false, {:name => "line_item[item_id][]", :id => "line_item_id_#{item.id}" } %><label for="line_item_id_<%= item.id %>"> <%= item.title %></label></li>
<% end %>
</ul>
Make sure to use check_box_tag instead of check_box. check_box holds a hidden text input that by default inserts a 0 into the database. From http://rubyonrails.org/api/classes/ActionView/Helpers/FormHelper.html
The checked_value defaults to 1 while the
default unchecked_value is set to 0 which is convenient for
boolean values. Usually unchecked checkboxes don’t post anything. We
work around this problem by adding a hidden value with the same name as the
checkbox.
You don’t want that. You want the plain vanilla check_box_tag which does none of that nonsense, because you in fact, don’t want your line_item table being filled with up with all kinds of line_items referring to product "0" or product "NULL".
So that’s our form. The line <% for item in @items %>
comes from the items.rb model and is just a little database query to get all the items associated with a particular order for posting in our invoice. Why would we use checkboxes? Well, maybe we’re not going to invoice the whole order. Maybe we’re out of some items. We’ll let our warehouse guy check off the checkboxes on his wireless pda. How’s that?
If you were watching closely, you’ll notice that I modified the check_box_tag behavior with options of my own with the following:
:name => "line_item[item_id][]"
This is what gives us multiple lines (an array of items) to pass to the controller. [] is the important part.
Now, so far this is easy, or at least I thought so. I’ve done this a hundred times in php, but that’s just the problem, I got tired of writing and re-writing this. I wanted Rails to handle all the parent child relationships for me and leave me alone. I’m lazy.
But I couldn’t figure out exactly how to do this. Frankly, I’m still trying to fit all the method/class/object/instance/variable blah blah blah into my head and keep all the Invoice invoice invoices straight. I know, I know, it’s probably me, but I’ll wager there are a few more slow-witted programmers out there for whom this is all so confusing. A phrase that I have been becoming more familiar with while working in Ruby on Rails is, "Use the force." It’s funny, but most of the time when I relax and make stuff up without trying to "understand," things usually Just Work(TM). Jedi Programming… who knew?
So we’ve got our form. Now we need to post the parent and the children in one fell swoop.
Now for the model (no, not Victoria’s Secret): Invoice will not reference the children (the children will come running when they hear their parent’s voice regardless of whether they are called by name). The parent "has_many" children and does not bother remembering their names or ids or anything. The children on the other hand "belong_to " (or reference) the parent and are tattooed with the parent_id stamp of ownership (big ears for example). When they are required, they will all line up under the parent and file out like good little children.
Got it? Parent -> has_many :children, Child -> belongs_to :parent – the model of a perfect Catholic Rails family.
Now we need to post the stuff. This is a snippet from the invoice_controller.rb:
def create
@invoice = Invoice.new(params[:invoice])
@invoice.order_id = @session["order_id"]
for item_id in params[:invoice_item][:item_id] do
@invoice.invoice_items << InvoiceItem.new(:item_id => item_id)
end
if @invoice.save
flash[:notice] = 'Invoice was successfully created.'
redirect_to :action => 'list'
else
render :action => 'new'
end
end
order_id is stored in the session array and is used to reference the invoice. The invoice in turn has items added to it for each item_id in the params passed from our form. What happens on @invoice.save is the following:
- Rails inserts the invoice header (the parent)
- Immediately fetches currval(invoices_id_seq) to retrieve the newly created invoice_id
- Uses that invoice_id number and iterates over the invoice_items inserting both the item_id and invoice_id
- commits the results if successful
That’s it! Easy, huh? Well it took me all day to figure it out. I knew it was easy, but perhaps I don’t have mad google sklz or something, because it left me scratching my head. Hopefully someone will find this useful. Leave a comment and I’ll do my best to answer your questions. If not, I’m sure I’ll forget it in a few months and have to reread this *G*.