I'm a Ruby on Rails / jQuery web developer. Follow me at @sikachu

Archive for the ‘Ruby’ Category

Possible pitfall on ActiveRecord::Base#create

Saturday, August 21st, 2010 Posted in Ruby, Ruby on Rails | View Comments

I came across this a while ago when I was trying to clean up my code. Just write it down so you won't follow me :) Consider that Post having :title attribute and class Post < ActiveRecord::Base validates_presence_of :title end Well, what do ...

Ruby 1.9.2 is out!!

Wednesday, August 18th, 2010 Posted in News, Programming, Ruby, Ruby on Rails | View Comments

Just got the news in Twitter today that Ruby 1.9.2 is out! This is the Ruby that we've been long-awaits which shouldn't have any strange compatibility issue with the upcoming Rails 3, and wayyyy faster than 1.8.7. The timing couldn't be ...

เจออะไรใน Rails 3: เพิ่มพลัง ActiveRecord#find (ตอนที่ 2)

Friday, February 12th, 2010 Posted in Ruby, Ruby on Rails | View Comments

เจออะไรใน Rails 3 เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0 หลังจากที่ผมนำเสนอเรื่องของการ deprecate options ทั้งหมดของ ActiveRecord#find แล้วเปลี่ยนเป็นเมธอดที่ทำหน้าที่คล้ายๆ กับ named_scope กันไปแล้ว ตอนนี้ผมไปเจอข้อมูลจาก @lifo ซึ่งพูดเพิ่มเติมในเรื่องของเมธอดที่จะถูก deprecated และเมธอดที่ให้ใช้แทนครับ สิ่งที่จะถูกถอดออกไป การเรียกใช้ #find โดยมี option hash นั้นจะ deprecated ออกไป User.find(:first, :where => {:status => "suspended"}) แต่อย่างไรก็ตาม #find method จะยังคงอยู่ โดยคุณสามารถใช้มันเพื่อหา record ตาม ...

เจออะไรใน Rails 3: เข้ารหัส cookie และสร้าง cookie ที่ไม่มีวันหมดอายุอย่างง่ายๆ

Thursday, February 11th, 2010 Posted in Ruby, Ruby on Rails | View Comments

เจออะไรใน Rails 3 เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0 เมื่อก่อนนี้ ถ้าหากเราต้องการที่จะทำ cookie ที่ไม่มีวันหมดอายุ (เช่น remember me) เราจำเป็นที่จะต้องกำหนดเวลาหมดอายุของ cookie เอง เช่น cookies[:authorization_key] = { :value => @user.authorization_key, :expires => 20.years.from_now.utc } แต่หลังจาก commit ของ DHH อันนี้ ทำให้เราสามารถเขียนใหม่ได้เป็น cookies.permanent[:authorization_key] = @user.authorization_key นอกจากนั้น เพื่อเพิ่มความปลอดภัยให้กับ Cookie เราก็ยังสามารถที่จะจับมัน signed ...

เจออะไรใน Rails 3: ใส่ flash message ลงไปใน redirect_to ได้เลย

Wednesday, January 20th, 2010 Posted in Ruby, Ruby on Rails | View Comments

เจออะไรใน Rails 3 เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0 ความเป็นมา ปกติแล้วใน Rails 2 ถ้าเราต้องการที่จะตั้งค่า flash message ก่อนที่จะทำการ redirect เราจำเป็นทีจะต้องตั้งค่ามันก่อนที่จะเรียก redirect_to เช่น class UsersController < ApplicationController def create @user = User.new(params[:user]) if @user.save ...

เจออะไรใน Rails 3: เพิ่มพลัง ActiveRecord#find

Saturday, January 2nd, 2010 Posted in News, Ruby, Ruby on Rails | View Comments

เจออะไรใน Rails 3 เป็นสกู๊ปพิเศษสำหรับนำเสนอสิ่งใหม่ๆ ที่จะมีเพิ่มขึ้นมาใน Ruby on Rails 3.0 ความเป็นมา หลายคนนั้นคงจะรู้จักกับฟีเจอร์ named_scope ที่ได้เปิดตัวออกมาพร้อมกับ Rails 2.1 ทำให้เราสามารถที่จะเขียน scope แล้วนำ scope นี้ไปใช้ได้ใหม่เรื่อยๆ เป็นไปตามหลักของ DRY เช่น class Post < ActiveRecord::Base named_scope :published, :conditions => { :status => :published } named_scope :latest, lambda { |limit| ...

Operator Precedence ใน Ruby

Saturday, December 19th, 2009 Posted in Programming, Ruby | View Comments

Operator Precedence หรือลำดับสำคัญของตัวดำเนินการนั้น ถือเป็นหลักสำคัญของโปรแกรมเมอร์ ที่จะทำให้สามารถทำการเขียนอัลกอริธึมเพื่อแก้ไขปัญหาต่างๆ ได้ ในทางคณิตศาสตร์นั้น มักมีคนสรุปมันออกมาง่ายๆ ว่า "วงเล็บมาก่อน คูณหารก่อนบวกลบ" ถ้าใครเคยเห็น code ภาษา Ruby นั้น คงจะเคยเห็นว่า Ruby นั้นมีทั้ง operator && และ || เหมือนกับในภาษา C แล้วยังมี and กับ or ที่เพิ่มเข้ามาใน Ruby ซึ่งเป็นสิ่งที่ทำให้โค้ดนั้นดูสวยงามขึ้น อ่านง่ายขึ้นตามหลักของ Ruby ลองดูโค้ดด้านล่างนี้เป็นตัวอย่างครับ irb(main):001:0> dessert = "Cheesecake" => "Cheesecake" irb(main):002:0> drink = "Coffee" => ...

Optimize database query ด้วย :include

Friday, August 7th, 2009 Posted in My Project, Programming, Ruby, Ruby on Rails | View Comments

ในการเขียนโปรแกรมบน Ruby on Rails นั้น เรามักที่จะใช้ ActiveRecord ในการทำหน้าที่เป็น ORM ระหว่างตัว Application กับ database ซื่งทำให้การเรียก Record นั้น สามารถทำได้อย่างง่ายดาย เช่น ถ้าผมจะเรียกดู post ทั้งหมดที่มีอยู่ในระบบ ผมแค่สั่ง Post.find(:all) # หรือว่า Post.all ก็ได้ ใน Rails 2.x ซึ่งตรงนี้ ถ้าเราไปดูใน Log file จะพบว่า ActiveRecord นั้น จะใช้คำสั่งค้นหาข้อมูลประมาณนี้ครับ ...

Thin Rolling Restart Patch

Sunday, August 2nd, 2009 Posted in My Project, Programming, Ruby, Ruby on Rails | Comments Off

I wrote a patch for Thin web server to do a rolling restart (i.e. restart the server one at a time) when I was working for my final thesis, Localmapia. I put my source on Github, and already inform the ...

Object#try ใน Rails 2.3

Sunday, August 2nd, 2009 Posted in My Project, Programming, Ruby, Ruby on Rails | Comments Off

เคยเจอปัญหาบ้างไหมครับ กับการที่บางครั้ง object ที่เราเรียก method ไปเนี่ย มันกลายเป็น nil ขึ้นมา ทำให้เกิด exception ขึ้นมา >> @user.username NoMethodError: You have a nil object when you didn't expect it! The error occurred while evaluating nil.username from (irb):1 ซึ่งตรงนี้ เพื่อที่จะหลบ exception ในบางครั้งทำให้ Developer ต้องทำการเช็คก่อนว่า object นั้นเป็น nil หรือไม่ เช่น >> (@user ...