NodeJS

อาทิตย์ที่แล้วได้ยินเกี่ยวกับ NodeJS ก็เลยลองค้นๆดู ลองดู code แล้วก็วีดีโอรู้สึกว่ามันเจ๋งจริงๆ ไอ้เจ้า NodeJS เนี่ยมันทำให้สามารถเขียน server-side program ด้วย Javascript ได้และยังเอาข้อดีของ Javascript มาใช้ด้วย ในวีดีโอเค้าเริ่มมาด้วยการบอกว่า I/O นั้นต้องทำใหม่ให้ต่างออกไป เพราะตอนนี้เวลาเรียกข้อมูลจากฐานข้อมูล หรือเรียก web services อื่นๆ เราไม่ได้ทำอะไร ทำให้เสีย CPU cycle ไปโดยเปล่าประโยชน์ แถมเค้ายังเปรียบเทียบกับ Apache ให้ดูว่าหน่วยความจำที่ใช้นั้นมันต่างกันมากขนาดไหน ทั้งนี้ก็เพราะว่า Apache ใช้การสร้าง Thread ใหม่ให้กับทุกการเชื่อมต่อ แต่ NodeJS ใช้ event loop ซึ่งเป็น single thread ทำให้กินหน่วยความจำน้อยกว่ามาก

จากที่ดูตัวอย่าง chat room แล้ว การใช้ call back มันทำให้งานแบบนี้ง่ายขึ้นมาก เพราะเค้าเล่น call ไปก่อนเลย ถ้ามีข้อความใหม่ก็ส่งมาทันทีพอได้รับก็ส่งไปค้างไว้ใหม่ ไม่ต้องคอยตั้งเวลาส่งไปถาม server ว่ามีข้อความใหม่รึเปล่าอยู่เรื่อยๆ แถมอัพเดททันใจอีกต่างหาก

ดูแล้วเทคโนโลยีนี้ก็น่าจะช่วยลดต้นทุนในการดำเนินการได้มากทีเดียว เนื่องจากเราสามารถรองรับผู้ใช้ได้มากขึ้นโดยหน่วยความจำเท่าเดิม ก็ไม่จำเป็นจะต้องเพิ่มจำนวนเครื่องแถมประัหยัดไฟอีก ไว้เดี๋ยวจะไปลองเล่นดูแล้วจะมาเขียนใหม่


Macbook Pro

I have to develop an iphone app for my master project so I bought Macbook Pro.

I'm a Mac and I'm a PC. :D


Unreliable Transportation

I've been living in USA for 1 year. I think transportation system in Pittsurgh is pretty good. However, Yesterday, I had very bad experience with the bus system. In the morning, the bus didn't stop at the bus stop because the driver thought I would like to go another way. How did he know where I want to go?!?!?!?! Then, in the afternoon, a bus came about 10 minutes early! and I missed it. I checked the schedule and I have to wait 15 minutes more, ok I can wait, but it came 10 minutes late......UNRELIAB LE!



I really want to eat thai noodle but I can't find the restaurant that make a good one. So I made it myself :D.



Training

This week, I trained users how to use the application I've developed. I can feel that they like it. However, they want me to add more functions and want to change report format.

I've talked to my supervisor, she told me that this application will reduce time that managers have to spend on this task. Also, the old system, they can work on it only one person at a time. That means other managers have to wait until a manager who is using it finish the task.

Nest week, I will train content creators to make webpages with CSS because the old web pages they have don't use CSS and when they want to change their web theme they have to change every single page. So, I suggest that they should use CSS and my supervisor agree with me. :D

I'm glad to see that the application I developed can save manager's time and make things better.


Many-to-Many with has_many :through

I'm working on my new project and it has many-to-many association. Also, I want to store additional fields in the relation. So, I used has_many :through in my models.

Example:

GIVEN: I have 2 models which are driver and bus. Driver can drive more than one bus and has start time and stop time for each bus. Also, bus can be drived by many drivers. ( Driver will drive particular bus only one period)
I will generate one more model called " Shift " which has bus_id, driver_id, start_time and stop_time. 

Model:

class Shift < ActiveRecord::Base
belongs_to :bus
belongs_to :driver
end
class Driver < ActiveRecord::Base
has_many :shifts
has_many :buses, :through => :shifts
end
class Bus < ActiveRecord::Base
has_many :shifts
has_many :drivers, :through => :shifts
end

Then, how to access start and stop time?

We treat Shiftas one model, so we can use
s = Shift.find_by_bus_id_and_driver_id(busID,driverID)
to get the shift and s.start_time or s.stop_time to access the additional fields.

I love Rails.