Rails: new, create, build

no comments

เวลาเขียน Rails หลายคนคงงงว่าในแต่ละอย่างมันต่างกันยังไง ลองยกตัวอย่างในแต่ละแบบดู

@user = User.new

สร้าง instance ของ Model User

@user = User.create(@attrs)

เหมือนกับ new แต่ข้อมูลจะถูกบันทึกลงไปในฐานข้อมูลเลย

@user = User.create!(@attrs)

เหมือนกับ create แต่ถ้าเกิด errors ขึ้นมา จะ return exceptions

@post = @user.posts.build

อันนี้จะเหมือนกับ new แต่ Rails จะสร้าง Foreign Key ให้กับ post โดยอัตโนมัติ

@post = @user.build_posts

ถ้า user และ post มี relation เป็น “has_one” ก็ใช้แบบนี้

Spotify business model

no comments

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

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

1. บริการ Free สามารถฟังเพลงได้แบบ unlimited โดยที่จะมีโฆษณาสอดแทรกนิดหน่อย

2. บริการแบบ Unlimited (4.99$ ต่อ เดือน) คือ เหมือนบริการแบบ Free แต่ไม่มีโฆษณา

3. บริการแบบ Premium (9.99$ ต่อ เดือน) คือ เหมือนบริการแบบ Unlimited แต่สามารถ streaming ผ่านมือถือได้ และฟังแบบ Offline mode ได้

ทีนี้มาดูกันว่าคนทำเพลงจะมีรายได้จาก Spotify ยังไง

Spotify จะจ่าย 0.003 EUR ต่อการเล่นเพลงนั้น

ถ้าฟังเพลงทั้ง Album Spotify จะจ่าย 0.029 EUR ต่อ Album

ถ้าคิดดูคราวๆ ฟัง Album 10 ครั้ง จะได้เงิน 0.29 EUR ถ้า 100 ครั้ง ได้ 2.9 EUR ถ้า 1000 ครั้ง (ฟังทุกวัน วันละครั้งเป็นเวลา 3 ปี!) จะได้ 29.47 EUR!

จะเห็นว่าแม้จะเป็นตัวเลขที่้น้อย แต่ถ้ามีจำนวนคนใช้บริการมากๆ และถ้าเป็นเพลงที่คนฟังบ่อย ตัวเลขรายได้นั้นไม่น้อยเลยทีเดียว

ทีนี้ลองเปรียบเทียบกับบริการอื่นๆ ดูบ้าง

DEEZER จะจ่าย 0.006 EUR ต่อการเล่นเพลงนั้น

Amazon และ iTune จะแบ่ง 70% – 30% โดยเจ้าของเพลงได้ 70% ต่อการซื้อเพลงหนึ่งครั้ง

Error: Unexpected EOF in archive (Cygwin, npm, node.js)

no comments

Some errors may occur while running “npm” install. The “unexpected EOF in archive” can be solved by the following:

1. Delete all the temp file by running the command

rm -R /tmp/*

2. Clear npm’s cache

npm cache clean

3. Close all of your current shell and all programs running on Cygwin

4. Run “cmd.exe” and start “ash” or “dash” shell (/cygwin/bin)

5. Execute the “rebaseall’ command

/bin/rebaseall -v

6. Exit and restart the new shell

7. Run “npm’ command again

npm install

ภาษาไทยบน HP TouchPad

8 comments

พอดีได้ HP TouchPad มาในราคา 99 ดอลลาร์ หลังจาก HP ประกาศเลิกทำสาย Hardware เลยประกาศลดราคา หลังจากลองเล่นๆ ดูก็พบว่าตัว WebOs 3 นั้น support ตัว Unicode แต่ใน font นั้นยังไม่มีตัวหนังสือภาษาไทย ถ้าใครอย่ากลง font ภาษาไทย ให้ทำตามขั้นตอนดังนี้

1. ให้ลง WebOS Quick Install บนเครื่องคอมพิวเตอร์

2. ให้ Download thai font จาก link นี้

3. เปิดโปรแกรม WebOS Quick Install

4. จากนั้นให้ไปที่ HP TouchPad ให้เข้า Developer Mode โดยการพิมพ์ “webos20090606″ ในช่อง Just Type

5. ให้ต่อเครื่อง HP TouchPad เข้ากับเครื่องคอมพิวเตอร์

6. จากเมนู เลือก Tool -> Send File ใน WebOs Quick Install

7. เลือกไฟล์ Heisei_Kaku_Gothic_nb.ttf ที่ download มา

8. เลือก Destination Folder เป็น /usr/share/fonts

9. หลังจากนั้นให้กดปุ่ม Send

10. Restart ตัว HP TouchPad

หลังจากนั้นตัว HP TouchPad ก็สามารถแสดงภาษาไทยได้ครับ

Defining the blog

no comments

I’ve created this blog to collect things happening to me from my “yesterday” (It is the name of the Slot Machine band’s song) . So I use the word to be the name of this blog. My mostly interests are technology, programming, computer science, photography, music and movie. You will meet those topics in my articles. I will use 2 languages to write articles, Thai and English. The language I use will be selected from the nature of the topic. I will decide it based on local and global paradigm.  If your interests look similar to me or any articles you have some opinions, please write some comments to share your thought, opinions or suggestions. Thank you!!

Hello world!

no comments

Hello world. This is my first blog in this domain. Start it with the hot air balloon picture!!!