Chapter 9
This chapter introduces the has_many concept, so I’m hoping Ackbar/KirbyBase don’t let me down. :)
Page 102. I need to create an “orders” table and add a new column to the “line_items” table. So, I edit create_db.rb. Here’s the code for the new “orders” table:
db.drop_table(:orders) if db.table_exists?(:orders)db.create_table(:orders,:name, :String,:email, :String,:address, :String,:pay_type, :String)
And here’s the modified create statement for “line_items”:
db.create_table(:line_items,:product_id, :Integer,:order_id, :Integer,:quantity, {:DataType => :Integer, :Default => 0},:unit_price, :Float)
I rerun create_db.rb to make the necessary database changes. I modify the Order class to let it know that it has_many :line_items. I modify the LineItem class to let it know that it belongs_to both :product and :order.
I create the Checkout form. I create the save_order method in the store controller. I put all the little touches in like validating the presence of the Order fields, showing errors, etc.
Page 109. Now for the moment of truth. I add some books to my cart and go to checkout. I enter the order info on the checkout screen, cross my fingers, and hit the “Checkout” button. My browse goes back to the store index with a message saying “Thank you for your order.”. Great! But did it really put the order and line_items in the database? It’s easy to find out.
I change directory over to db/development and open up orders.tbl in a text editor. Here’s what I see:
000001|000000|Struct|recno:Integer|name:String|...1|John Doe|jdoe@email.com|123 Robin Lane|po
Look’s good! Now, how about the line_items table:
000002|000000|Struct|recno:Integer|product_id:Integ...1|2|1|2|39.992|3|1|1|25.99
Fantastic!
The rest of the chapter involves making things a little prettier, so it doesn’t have any bearing on our experiment.
I’m done with Chapter 9 and I’m very happy with Ackbar. Assaph has done a great job. I have two more chapters to get through. I can see the finish line up ahead!
Jayson replied:
magnificent points altogether, you just received a logo new reader.
What might you recommend about your post that you simply made some days ago?
Any sure?
April 23, 2013 at 12:25 am. Permalink.
Walter replied:
Hi there to every single one, it’s truly a good for me to pay a quick visit this site, it includes priceless Information.
May 14, 2013 at 12:07 am. Permalink.