<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>fleuria (fleuria)</title>
    <link>https://ruby-china.org/fleuria</link>
    <description>don't feed the trolls</description>
    <language>en-us</language>
    <item>
      <title>ActiveRecord Transaction 的疑问</title>
      <description>&lt;p&gt;隐隐约约感觉文档里的这个例子有坑：&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ActiveRecord::Base.transaction do
  david.withdrawal(100)
  mary.deposit(100)
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;假设 withrawal 与 deposit 的实现是这样：&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def withraw
  self.balance -= 10
  self.save!
end

def deposit
  self.balance += 10
  save!
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;那么，david 和 mary 中 balance 字段的值是在进入 transaction 之前被取出的，如有竞态条件，无法保证事务的 Isolation。为此改成这样子心里可能更踏实一点：&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ActiveRecord::Base.transaction do
  david.reload
  mary.reload
  david.withdrawal(100)
  mary.deposit(100)
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;但是感觉略丑，有点拿不准 rails 有没有在这方面做特殊处理，或者有没有更简短的 api 呢？或者有没有使用 transaction 的注意事项或者最佳实战？&lt;/p&gt;

&lt;p&gt;thanks&lt;/p&gt;</description>
      <author>fleuria</author>
      <pubDate>Tue, 18 Feb 2014 08:56:04 +0800</pubDate>
      <link>https://ruby-china.org/topics/17321</link>
      <guid>https://ruby-china.org/topics/17321</guid>
    </item>
    <item>
      <title>mruby 稳定版 1.0.0？</title>
      <description>&lt;p&gt;&lt;a href="http://forum.mruby.org/download/index.html" rel="nofollow" target="_blank"&gt;http://forum.mruby.org/download/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;github 上也有了 1.0.0 的 tag，&lt;a href="https://github.com/mruby/mruby/tree/1.0.0" rel="nofollow" target="_blank"&gt;https://github.com/mruby/mruby/tree/1.0.0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;不过有点疑惑，forum.mruby.org 这个网站打包的 1.0.0 稳定版是不是官方的？看社区里也有朋友有类似的疑惑： &lt;a href="https://github.com/mruby/mruby/pull/1690" rel="nofollow" target="_blank"&gt;https://github.com/mruby/mruby/pull/1690&lt;/a&gt;&lt;/p&gt;</description>
      <author>fleuria</author>
      <pubDate>Sat, 08 Feb 2014 20:20:03 +0800</pubDate>
      <link>https://ruby-china.org/topics/17124</link>
      <guid>https://ruby-china.org/topics/17124</guid>
    </item>
    <item>
      <title>今晚 Light Talk 分享 mruby‎ GC 的 slides</title>
      <description>&lt;p&gt;&lt;a href="http://fleurer-lee.com/slides/mruby-gc/" rel="nofollow" target="_blank"&gt;http://fleurer-lee.com/slides/mruby-gc/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;第一次做分享，感觉没大拿准节奏 T T&lt;/p&gt;</description>
      <author>fleuria</author>
      <pubDate>Sun, 27 Oct 2013 23:25:58 +0800</pubDate>
      <link>https://ruby-china.org/topics/15073</link>
      <guid>https://ruby-china.org/topics/15073</guid>
    </item>
    <item>
      <title>生成随机字符串的一行代码</title>
      <description>&lt;p&gt;昨天不小心在 stackoverflow 看到的，很有趣：&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;random_str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;rand&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;len&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;update: 经 &lt;a href="/fcicq" class="user-mention" title="@fcicq"&gt;&lt;i&gt;@&lt;/i&gt;fcicq&lt;/a&gt; 君提醒，这个算法有个问题。大家想想修补的方法吧 :D  &lt;/p&gt;</description>
      <author>fleuria</author>
      <pubDate>Sun, 08 Jul 2012 14:02:10 +0800</pubDate>
      <link>https://ruby-china.org/topics/4187</link>
      <guid>https://ruby-china.org/topics/4187</guid>
    </item>
    <item>
      <title>大家说说曾遇到过的 rails 的坑与相关 tips 吧</title>
      <description>&lt;p&gt;意料不到的问题总会遇到，记一下的话再掉进同一个坑里就不那么容易了 :D&lt;/p&gt;

&lt;p&gt;先抛个砖，都是来自平时记录的一个&lt;a href="https://gist.github.com/1605918" rel="nofollow" target="_blank" title=""&gt;gist&lt;/a&gt; &lt;/p&gt;
&lt;h2 id="rvm"&gt;rvm&lt;/h2&gt;
&lt;p&gt;before &lt;code&gt;rvm install 1.9.2&lt;/code&gt;, make sure&lt;/p&gt;

&lt;p&gt;sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion&lt;/p&gt;
&lt;h2 id="Passenger"&gt;Passenger&lt;/h2&gt;
&lt;p&gt;跑&lt;code&gt;passenger-install-apache2-module&lt;/code&gt;时出来的提示&lt;code&gt;sudo apt-get install libopenssl-ruby&lt;/code&gt;是坑爹的，跟本没有。&lt;/p&gt;

&lt;p&gt;解决方法：&lt;/p&gt;

&lt;p&gt;rvm remove 1.9.2
    rvm pkg install openssl
    rvm install 1.9.2 --with-openssl-dir=$HOME/.rvm/usr&lt;/p&gt;
&lt;h2 id="bundle"&gt;bundle&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;bundle install RAILS_ENV=production&lt;/code&gt;是个错误的命令，因为 bundle 会把后面的都当成安装 gem 的目标路径，而 bundle 本身并不存在 RAILS_ENV 这回事。它会产生一个名为./RAILS_ENV 的文件夹，并拿它作为以后不 bundle 安装的默认路径。&lt;/p&gt;

&lt;p&gt;如果不慎打错了，解决方法：&lt;code&gt;rm .bundle -rf&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd `bundle show rails`&lt;/code&gt;可以 cd 到某 gem 所在的目录。&lt;/p&gt;
&lt;h2 id="RAILS_ENV"&gt;RAILS_ENV&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rails s -e production;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rails c production;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rake xxx:run RAILS_ENV=production;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="capistrano"&gt;capistrano&lt;/h2&gt;
&lt;p&gt;对于那种『有可能错误但错了也不碍事』的命令，可以在后面加一个&lt;code&gt;; true&lt;/code&gt;，比如&lt;/p&gt;

&lt;p&gt;run "kill -QUIT &lt;code&gt;cat #{deploy_to}/shared/pids/unicorn.pid&lt;/code&gt;; true" &lt;/p&gt;
&lt;h2 id="APP_CONFIG"&gt;APP_CONFIG&lt;/h2&gt;
&lt;p&gt;有时在 application.rb 下加上这一行：&lt;/p&gt;

&lt;p&gt;APP_CONFIG = YAML.load_file("#{Rails.root}/config/app_config.yml")&lt;/p&gt;

&lt;p&gt;在 production 模式下是有坑的。&lt;code&gt;YAML.load_file&lt;/code&gt;的结果是一个字符串为 key 的哈希，如果用 symbol 取东西就会出错（但在 development 模式下一切正常）。解决方法是换成 ActiveSupport 中的&lt;code&gt;HashWithIndifferentAccess&lt;/code&gt;：&lt;/p&gt;

&lt;p&gt;APP_CONFIG = HashWithIndifferentAccess.new YAML.load_file("#{Rails.root}/config/app_config.yml")&lt;/p&gt;
&lt;h2 id="Migration"&gt;Migration&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;add_column&lt;/code&gt;时如果不小心把&lt;code&gt;boolean&lt;/code&gt;敲成&lt;code&gt;bool&lt;/code&gt;，会把 sqlite 的的&lt;code&gt;db:schema:dump&lt;/code&gt;搞坏掉，而 sqlite 并不允许更改 column 的名字。只能用一种蛋疼的&lt;a href="http://stackoverflow.com/questions/969187/altering-sqlite-column-type-and-adding-pk-constraint" rel="nofollow" target="_blank" title=""&gt;work around&lt;/a&gt;。&lt;/p&gt;
&lt;h2 id="Mongodb"&gt;Mongodb&lt;/h2&gt;
&lt;p&gt;ubuntu 源里的&lt;code&gt;mongodb-server&lt;/code&gt;是坑爹的，版本太低，正确的是&lt;code&gt;mongodb-10gen&lt;/code&gt;，需要加个&lt;a href="http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages" rel="nofollow" target="_blank" title=""&gt;源&lt;/a&gt;。&lt;/p&gt;
&lt;h2 id="Misc"&gt;Misc&lt;/h2&gt;
&lt;p&gt;一段&lt;a href="http://imtx.me/archives/1516.html" rel="nofollow" target="_blank" title=""&gt;小脚本&lt;/a&gt;，无关 rails，但可以提升跑 rake task 时的来福值。  &lt;/p&gt;</description>
      <author>fleuria</author>
      <pubDate>Thu, 22 Mar 2012 10:58:06 +0800</pubDate>
      <link>https://ruby-china.org/topics/2051</link>
      <guid>https://ruby-china.org/topics/2051</guid>
    </item>
  </channel>
</rss>
