<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>anklos</title>
    <link>https://ruby-china.org/anklos</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>�Nested Attributes 的一个问题</title>
      <description>&lt;p&gt;我有 2 个类&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Post
  has_and_belongs_to_many :receivers
  accepts_nested_attributes_for :receivers
end 
&lt;/code&gt;&lt;/pre&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Receiver
  has_and_belongs_to_many :posts
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;表的设计是：
&lt;code&gt;posts: id, xxx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;receivers: id, email, name, xxxx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;posts_receivers: post_id, receiver_id&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;完全遵循这个 &lt;a href="http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association" rel="nofollow" target="_blank"&gt;http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;在我的 form 里，每个 post 都会填上几个 receiver，有时候会有同一个的 receiver 的 email 在不同的 post 里出现。这个时候我不希望我的 receiver 表中对同样的 email 新建 record(因为 nested attributes 每次会自动新建一个 receiver)，而是在&lt;code&gt;posts_receivers&lt;/code&gt;表中根据已有的 email 的 receiver id 去建新的 record 来存储新的 post 和已有的 receiver 的关系。&lt;/p&gt;

&lt;p&gt;请教有什么比较好的方法能实现呢？非常感谢！&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Fri, 26 Feb 2016 09:36:55 +0800</pubDate>
      <link>https://ruby-china.org/topics/29110</link>
      <guid>https://ruby-china.org/topics/29110</guid>
    </item>
    <item>
      <title>在 view 里 will_paginate 一个数组</title>
      <description>&lt;p&gt;生产环境：&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gem 'rails', '4.1.0'
gem 'will_paginate', '~&amp;gt; 3.0'

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;似乎在 view_helper 的&lt;code&gt;will_paginate&lt;/code&gt;只支持&lt;code&gt;ActiveRecord::Relation&lt;/code&gt;类型，不支持数组？不像在其他地方你可以&lt;code&gt;require 'will_paginate/array'来用&lt;/code&gt;an_array.paginate(...)`&lt;/p&gt;

&lt;p&gt;有办法能在 view 让 will_paginate 里用数组类型的数据吗？谢谢！！&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Sun, 12 Oct 2014 20:59:59 +0800</pubDate>
      <link>https://ruby-china.org/topics/22001</link>
      <guid>https://ruby-china.org/topics/22001</guid>
    </item>
    <item>
      <title>如何排序 ActiveRecord::Relation</title>
      <description>&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Post
  has_many :comments
end
&lt;/code&gt;&lt;/pre&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Comment
  # 表里有name这一栏
  belongs_to :post
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;我想选出根据&lt;code&gt;comments.size&lt;/code&gt;排序&lt;code&gt;post&lt;/code&gt;，然后返回的类型是&lt;code&gt;ActiveRecord::Relation&lt;/code&gt;.
写到这里写不下去了：&lt;code&gt;Post.includes(:comments).order('comment.size DESC')&lt;/code&gt;。不知应该怎么在&lt;code&gt;order&lt;/code&gt;里来表示排序。&lt;/p&gt;

&lt;p&gt;类似还有如果我想选出某一个人的&lt;code&gt;comment&lt;/code&gt;：&lt;code&gt;Post.includes(:comments).where('comment.name = ?',  name)&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;请教应该如何做？谢谢！&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Sun, 12 Oct 2014 19:41:17 +0800</pubDate>
      <link>https://ruby-china.org/topics/21999</link>
      <guid>https://ruby-china.org/topics/21999</guid>
    </item>
    <item>
      <title>Mac 上的 QQ 浏览器是什么情况</title>
      <description>&lt;p&gt;今天体验了下 mac 上的 qq 浏览器，号称是所有版本里做的最高端大气上档次了。我好奇的下载试用了下。&lt;/p&gt;

&lt;p&gt;我靠，感觉就是 chrome 的中文版，有没有？？&lt;/p&gt;

&lt;p&gt;右键打开‘审查元素’，当时我就震惊了，完全就是 chrome 原版有没有？！&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Fri, 18 Oct 2013 07:35:07 +0800</pubDate>
      <link>https://ruby-china.org/topics/14833</link>
      <guid>https://ruby-china.org/topics/14833</guid>
    </item>
    <item>
      <title>请正确理解。。三十而立</title>
      <description>&lt;p&gt;已经快三十了，一想起三十而立这个大坑，就感到压力山大。
估计版上不少每天奋斗在代码第一线的同学都有共鸣。&lt;/p&gt;

&lt;p&gt;这句话出自《论语·为政》：
子曰：吾十有五而志于学，三十而立，四十而不惑，五十而知天命，六十而耳顺，七十
而从心所欲，不逾矩。&lt;/p&gt;

&lt;p&gt;关于这段话，课本上写着的、老师们的嘴里说出的是看似很深奥的解释，
但我要说，必须纠正这种盲目崇拜古人的调调，
我认为这几句话其实很简单，
到目前为止理解最准确的其实是来自于天桥的相声艺人（哪段相声我忘了）&lt;/p&gt;

&lt;p&gt;这压根就是孔子贴在自己的私立学校门口的收费标准：&lt;/p&gt;

&lt;p&gt;三十而立---是说，只交三十文钱，那就只能站着听课；
四十不惑---是说，交足四十文钱，那就能提问了，而且可以一直问到没疑问为止；
五十知天命---是说，交够五十文钱，那就能提前知道明天的考试命题，必能事半功倍；
六十耳顺---是说，交够六十文前，你听什么话顺耳，夫子我就说什么，绝对让你开开
心心上学来，高高兴兴回家去；
七十随心所欲---就是说，交够七十文钱，那就享受 vip 待遇，在俺这一亩三分地，你想
咋折腾就咋折腾！&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Fri, 06 Sep 2013 11:56:43 +0800</pubDate>
      <link>https://ruby-china.org/topics/13922</link>
      <guid>https://ruby-china.org/topics/13922</guid>
    </item>
    <item>
      <title>如何从本机连接 vagrant 里的 mysql</title>
      <description>&lt;p&gt;我的设置步骤：&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;部署最基本的 vagrant&lt;/li&gt;
&lt;li&gt;vagrant ssh 进去做 &lt;code&gt;sudo apt-get install mysql-server&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;在 Vagrantfile 里加上 &lt;code&gt;config.vm.forward_port 3066, 4567&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;vagrant reload&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;另外在 vagrant 里加上一些其他的设置：&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;在/etc/hosts.allow 里加上：&lt;code&gt;mysqld: ALL : ALLOW&lt;/code&gt; 和 &lt;code&gt;mysqld-max: ALL : ALLOW&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;在/etc/mysql/my.cnf 里注释掉&lt;code&gt;skip-external-locking&lt;/code&gt;, 并且把&lt;code&gt;bind-address&lt;/code&gt;设置为&lt;code&gt;0.0.0.0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;在 mysql 里 GRANT 权限&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;弄好后从本机打入命令：mysql -uroot -h 127.0.0.1 -P 4567&lt;/p&gt;

&lt;p&gt;得到是&lt;code&gt;ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;这样 port forward 是没问题了，但是不知道为什么始终被中断链接？&lt;/p&gt;

&lt;p&gt;&lt;a href="/Saito" class="user-mention" title="@Saito"&gt;&lt;i&gt;@&lt;/i&gt;Saito&lt;/a&gt; &lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Wed, 17 Oct 2012 14:06:30 +0800</pubDate>
      <link>https://ruby-china.org/topics/6120</link>
      <guid>https://ruby-china.org/topics/6120</guid>
    </item>
    <item>
      <title>请教关于 assets pipeline 的一个问题</title>
      <description>&lt;p&gt;在给公司做个 gem，目的是自动生成一些可配置的 theme 模版。这样在很多小型网站开发种能重复调用，然后做一些不同的 config 来生成不同的页面。对 asset pipeline 不是很不熟悉，只有硬着头皮来。。&lt;/p&gt;

&lt;p&gt;现在生成模版大概的结构是：&lt;/p&gt;

&lt;p&gt;app/&lt;br&gt;
      themes/
        theme1/ 
          images/
          javascripts/
          stylesheets/
            theme1.css
            theme1.css.erb
          views/
            demo.html.erb
        theme2/
         …
        theme3/
         …&lt;/p&gt;

&lt;p&gt;在 controller 里，我可以这样：&lt;/p&gt;

&lt;p&gt;class MyAppController &amp;lt; ApplicationController&lt;/p&gt;

&lt;p&gt;theme 'theme1' #挑选 theme1&lt;/p&gt;

&lt;p&gt;config_theme {:ulr =&amp;gt; 'test', :background =&amp;gt; :img…}&lt;/p&gt;

&lt;p&gt;def demo
        render 'demo' #从 theme1 里拿到 demo 的页面
      end
    end&lt;/p&gt;

&lt;p&gt;遇到的问题是我无法调用到 theme1 下 stylesheets 里的 css。有几点：&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;prepend_view_path 无法让 css 在设定的路径下搜寻。&lt;/li&gt;
&lt;li&gt;即使我给绝对路径，仍然无法拿到 css。（我已经设置了&lt;code&gt;config.assets.paths &amp;lt;&amp;lt; "#{Rails.root}/app/themes"&lt;/code&gt;
), 因为 app/themes/不是 public?&lt;/li&gt;
&lt;li&gt;根据不同 controller 里配出来的 theme1.css.erb 是无法 precompile 的吧？那这种情况应该如何压缩或者其他优化方法？&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;谢谢！&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Thu, 19 Jul 2012 17:42:21 +0800</pubDate>
      <link>https://ruby-china.org/topics/4424</link>
      <guid>https://ruby-china.org/topics/4424</guid>
    </item>
    <item>
      <title>请教如何解决在 after_create block 中 id 是 0 的问题</title>
      <description>&lt;p&gt;遇到个问题，调试无果，想请教下大家。&lt;/p&gt;

&lt;p&gt;简单的说就是创建一个 model 后，在 after_create 中拿到的新创建的实例的 id 是 0（其他的属性的值都是正确的）。蛋疼的是，在同一个 project 里，有的 model 可以拿到正确的 id，有的拿不到。我比较了这些 model 和 controller，没发现什么特别不一样的地方。。&lt;/p&gt;

&lt;p&gt;另外在 debug 的时候，我可以从 belongs_to 的 parent 的实例里看到已经创建的这个实例，并且 id 也存在。但是 self.id 是 0。&lt;/p&gt;

&lt;p&gt;我自己新创建一个 rails3 的 project 调试过，在 after_create 是肯定能拿到 id 的。&lt;/p&gt;

&lt;p&gt;我试过 after_create，after_commit 里都拿不到正确的 id 值。&lt;/p&gt;

&lt;p&gt;环境是 rails 3.1.4&lt;/p&gt;

&lt;p&gt;请问下可能造成这种情况的原因是什么？谢谢！&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Wed, 23 May 2012 09:48:14 +0800</pubDate>
      <link>https://ruby-china.org/topics/3441</link>
      <guid>https://ruby-china.org/topics/3441</guid>
    </item>
    <item>
      <title>公司厕所信号调查</title>
      <description>&lt;p&gt;我工作过的三家公司，厕所里信号都非常差，几乎是 sos 状态。导致每天大号时间严重浪费！各位版友有没也遇到同样情况？&lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Mon, 23 Apr 2012 19:11:57 +0800</pubDate>
      <link>https://ruby-china.org/topics/2841</link>
      <guid>https://ruby-china.org/topics/2841</guid>
    </item>
    <item>
      <title>如何检测 runtime 中一个 instance 存在时间</title>
      <description>&lt;p&gt;只能程序里自己打表计算吗？  &lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Wed, 18 Apr 2012 12:33:03 +0800</pubDate>
      <link>https://ruby-china.org/topics/2678</link>
      <guid>https://ruby-china.org/topics/2678</guid>
    </item>
    <item>
      <title>项目里如何动态查找已存在的类？</title>
      <description>&lt;p&gt;项目里有个地方要动态的查找某个 string 是不是一个 class&lt;/p&gt;

&lt;p&gt;我发现 defined？的方法似乎要直接传类名才可以。&lt;/p&gt;

&lt;p&gt;比如：&lt;code&gt;defined?(String)&lt;/code&gt;的结果是'constant'，正确。
但是   &lt;code&gt;defined?('String'.constantize)&lt;/code&gt; 结果是‘method'。不解。irb 里 &lt;code&gt;String == 'String'.constantize&lt;/code&gt;的结果是 true。但同时我也发现&lt;code&gt;String ==＝ 'String'.constantize&lt;/code&gt;是 false 的。&lt;/p&gt;

&lt;p&gt;或者用 Module.const_get 这个方法去判断一个类是否存在，如果想判断诸如‘A::B::C'的在 module 下面的类的话，const_get 会抛 name error 的异常。&lt;/p&gt;

&lt;p&gt;目前就简单的 hack 出一个方法来帮助判断：&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;present?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;begin&lt;/span&gt; 
     &lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;
     &lt;span class="kp"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;rescue&lt;/span&gt;
     &lt;span class="kp"&gt;false&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;但觉得这种做法很烂。请问大家是如何处理这种情况的？  &lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Wed, 21 Mar 2012 18:52:15 +0800</pubDate>
      <link>https://ruby-china.org/topics/2034</link>
      <guid>https://ruby-china.org/topics/2034</guid>
    </item>
    <item>
      <title>为什么相比 mass assignment, sql injection 很多人都会注意</title>
      <description>&lt;p&gt;rails 里大家写 sql 查询时都会注意 sql injection 这个问题，但为什么 mass assignment 一直没得到太大关注呢？rails casts 早在 07 年就谈到这个问题阿。很不理解！  &lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Wed, 07 Mar 2012 07:28:39 +0800</pubDate>
      <link>https://ruby-china.org/topics/1666</link>
      <guid>https://ruby-china.org/topics/1666</guid>
    </item>
    <item>
      <title>周五下午，你们在公司做什么</title>
      <description>&lt;p&gt;我们是 weekly review，茶话会一般。后接乒乓，XOBX  &lt;/p&gt;</description>
      <author>anklos</author>
      <pubDate>Fri, 17 Feb 2012 10:37:45 +0800</pubDate>
      <link>https://ruby-china.org/topics/1251</link>
      <guid>https://ruby-china.org/topics/1251</guid>
    </item>
  </channel>
</rss>
