Ruby On Rails Pitfalls
Upcoming SlideShare
Loading in...5
×
 

Ruby On Rails Pitfalls

on

  • 1,362 views

Talk on Ruby Conf China 2009

Talk on Ruby Conf China 2009

Statistics

Views

Total Views
1,362
Views on SlideShare
1,359
Embed Views
3

Actions

Likes
0
Downloads
14
Comments
0

1 Embed 3

http://www.linkedin.com 3

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • action controller 相关的问题\nAnything you place in the flash will be exposed to the very next action and then cleared out.\n
  • Anything you place in the flash will be exposed to the very next action and then cleared out.\n
  • \n
  • action view相关\n
  • 在一些有特殊容错特性的浏览器中,比如IE 6.0\n不完整的tag会被拼成一个完整的tag.\n\n
  • not safe sanitizer:HTML::FullSanitizer\nsafe sanitizer:HTML::WhiteListSanitizer\n
  • controller + view\n代码来源于Agile Web Development With Rails\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • active record相关\n
  • \n
  • when there’s no table lock\n
  • when there’s no table lock\n
  • when there’s no table lock\n
  • when there’s no table lock\n
  • when there’s no table lock\n
  • when there’s no table lock\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 如果conditions是组合而成的,要注意是否有组合内容为空的情况.组合内容为空,conditions不能为空.否则,可能得到完全相反的结果.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • otherwise, something strange could happen\n
  • \n
  • \n
  • \n

Ruby On Rails Pitfalls Ruby On Rails Pitfalls Presentation Transcript

  • Ruby on Rails Pitfall Or just stupid mistakes we made Robin Lu IN-SRC Studio robinlu@in-src.com RubyConfChina2009
  • IN-SRC Studio• http://www.in-src.com• Team behind Caibangzi.com• Full stack Ruby On Rails Development• Projects from Pepboys,Vitality, Healthwise...
  • ‘and’ or ‘&&’What does this mean? result = func(arg) and render(:text => result)
  • ‘and’ or ‘&&’What does this mean? result = func(arg) and render(:text => result)Why not this? result = func(arg) && render(:text => result)
  • ‘and’ or ‘&&’What does this mean? result = func(arg) and render(:text => result)Why not this? result = func(arg) && render(:text => result)Be aware of the operator precedence
  • strip_tags Display user input text without tagsWhat we did:
  • strip_tagsWhen text = ‘<img title="http://example.com/x.js?" src="#"’the page becomes:<p> <img title="http://example.com/x.js?" src="#" </p>
  • strip_tagsstrip_tags is not safe by itself h strip_tags(text)
  • cacheclass Blog1Controller < ApplicationController def list unless read_fragment(:action => list) @articles = Article.find_recent Controller end endend<% cache do %> <ul> <% for article in @articles -%> <li><p><%= h(article.body) %></p></li> list.html.erb <% end -%> </ul><% end %>
  • cacheResult: sometime got crash due to uninitialized @articles
  • cachearticle list
  • cache article listcheck cache
  • cache article listcheck cache list
  • cache article listcheck cache list render
  • cache article list article newcheck cache list render
  • cache article list article newcheck cache list expire cache render
  • cache article list article newcheck cache list expire cache render
  • cache article list article newcheck cache list expire cache rendercheck cache
  • cache article list article new check cache list expire cache render check cachecrashed by non-init @articles
  • cacheSolutions? • defensive: handle the exception • postpone init of @articles • update caches instead of expiring them none of them is perfect
  • object id
  • object idCheck nil? everywhere?
  • object idconfig.whiny_nil = true
  • validate_uniqueness_of
  • validate_uniqueness_ofWe always get errors like this:A ActiveRecord::StatementInvalid occurred infund#add_watch_fund: Mysql::Error: Duplicate entry 1234-271 for key 2:INSERT INTO `watch_funds` (`account_id`,`position`, `fund_id`, `created_at`) VALUES(1234, 19,271, 2009-05-06 19:13:50)
  • validate_uniqueness_of Process A Process B
  • validate_uniqueness_of Process A Process B unique?
  • validate_uniqueness_of Process A Process B unique? select ....
  • validate_uniqueness_of Process A Process B unique? select .... unique?
  • validate_uniqueness_of Process A Process B unique? select .... unique? Insert
  • validate_uniqueness_of Process A Process B unique? select .... unique? Insert Insert
  • validate_uniqueness_of Process A Process B unique? select .... unique? Insert Insert crash!
  • validate_uniqueness_of validate_uniqueness_of may not guarantee the uniquenessuse your own lock if the uniqueness is critical to you.
  • conditionsBackground: • category has many subcategories • subcategory has many posts • post belongs to subcategorywe need to select all posts in a category.
  • conditionsWhat we did:named_scope :in_category, lambda { |cat| conditions = [cat.subcategories.map {|subcat| posts.subcategory_id = ? }.join(" OR ")] cat.subcategories.each {|subcat| conditions << subcat.id } {:conditions => conditions}}
  • conditionsResult: we get all posts when a category has no subcategories
  • conditionsWhen category has no subcategorynamed_scope :in_category, lambda { |cat| conditions = [cat.subcategories.map {|subcat| posts.subcategory_id = ? }.join(" OR ")] cat.subcategories.each {|subcat| conditions << subcat.id } {:conditions => conditions}}
  • conditions When you compose conditions, be aware that sometime nothing to compose meansthe conditions should match nothing,not the conditions should be empty.
  • before_create set a flag if the author of the post is an adminWhat we did:
  • before_createResult: Only post by admin can be saved
  • before_create All these callbacks are FiltersBe careful not to break the filter chain by what you return from the filters!
  • after_create send a mail whenever a new record is createdWhat we did:
  • after_createResult: sometime the record save failed but we still get mail notification
  • after_create before_create begin ... create ... commit after_createall in one transaction all the steps between this should be transactional
  • after_createWhat are non-transactional actions?• send a mail• delete a file• expire a cache
  • after_create• try not put non-transaction actions into transactions. • after_commit • in controller
  • Thanks!