<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>R_Rboys (Yibin.zheng)</title>
    <link>https://ruby-china.org/R_Rboys</link>
    <description/>
    <language>en-us</language>
    <item>
      <title>[求助] rails 版本 4.1，select 后的对象被保存在 association</title>
      <description>&lt;blockquote&gt;
&lt;p&gt;Rails 版本 4.1.1
database: pg&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;我把 rails 的版本从 4.0 升到 4.1，在调整项目代码的时候，遇到这样的一个问题&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ActiveRecord::Base
    has_one :user_info

    delegate :total_points, to: :user_info
end
&lt;/code&gt;&lt;/pre&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserInfo &amp;lt; ActiveRecord::Base
    belongs_to :user
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;当我执行下列代码的时候&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user = UserInfo.select(:user_id).first.user #=&amp;gt; #&amp;lt;User id: 26, ...... &amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;神奇的事情就这样出现了&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p user.user_info  #=&amp;gt; #&amp;lt;UserInfo id: nil, user_id: 26&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;因为 user.user_info 返回的是 select 后的对象，user.total_points 会报错：【missing attribute: total_points】&lt;/p&gt;

&lt;p&gt;补充：在 rails 4.0 里，user.user_info 不会返回 select 后的对象，而且重新去访问数据库，所以在 rails 4.0 是没问题&lt;/p&gt;

&lt;p&gt;求助，求助&lt;/p&gt;</description>
      <author>R_Rboys</author>
      <pubDate>Wed, 28 May 2014 14:16:22 +0800</pubDate>
      <link>https://ruby-china.org/topics/19593</link>
      <guid>https://ruby-china.org/topics/19593</guid>
    </item>
    <item>
      <title>请问下 attr_internal 会用在哪些情况下？</title>
      <description>&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class A
  attr_internal :name
  def my_name= name
    self.name = name
  end
  def my_name
    name
  end
end
a = A.new; a.name = "a"; a.instance_variables; #=&amp;gt; [:@_name]

class B &amp;lt; A; attr_accessor :name; end
b = B.new; b.name = "b"; b.instance_varialbes; #=&amp;gt; [:@name]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;使用 attr_internal 时，会生成一个&lt;a href="/_name" class="user-mention" title="@_name"&gt;&lt;i&gt;@&lt;/i&gt;_name&lt;/a&gt;的变量，其作用是为了防止子类的覆盖，如上面的 B
可是在上面的 B 类中，name 的方法已经给 attr_accessor :name 给覆盖了；那么当 b.my_name= "bb" 时，是&lt;a href="/name" class="user-mention" title="@name"&gt;&lt;i&gt;@&lt;/i&gt;name&lt;/a&gt; 被赋予了"bb"而不是&lt;a href="/_name" class="user-mention" title="@_name"&gt;&lt;i&gt;@&lt;/i&gt;_name&lt;/a&gt;；也就是说 attr_internal 的一点作用都没有，如果要一个&lt;a href="/_name" class="user-mention" title="@_name"&gt;&lt;i&gt;@&lt;/i&gt;_name&lt;/a&gt;的变量，那我直接定义就可以了，为什么要用 attr_internal 来定义呢？&lt;/p&gt;

&lt;p&gt;另外 attr_internal 会生成 name= 和 name 这两个方法；可是这两个方法会被 B 给覆盖，既然避免不了被覆盖，那我直接在 A 里用 attr_accessor 来定义就可以了；&lt;/p&gt;

&lt;p&gt;请问下有人能告诉我 attr_internal 具体的作用么？&lt;/p&gt;</description>
      <author>R_Rboys</author>
      <pubDate>Thu, 25 Apr 2013 20:06:34 +0800</pubDate>
      <link>https://ruby-china.org/topics/10506</link>
      <guid>https://ruby-china.org/topics/10506</guid>
    </item>
    <item>
      <title>请教个问题：路由是多文件好还是单文件？</title>
      <description>&lt;p&gt;上星期的时候，我这边的项目经理问了我一个问题：rails 支不支持将路由写在多个文件上？显然 rails 是支持的：&lt;a href="http://stackoverflow.com/questions/4930805/multiple-routing-file-in-rails-3" rel="nofollow" target="_blank"&gt;http://stackoverflow.com/questions/4930805/multiple-routing-file-in-rails-3&lt;/a&gt;
      那么我想请教个问题：路由是写在一个文件好呢还是写在多个文件好？&lt;/p&gt;

&lt;p&gt;补充：
      这里有这样的一种情况，假如你们公司想在国庆期间为自己的网站增加一些功能，把这些增加的功能所对应路由的写在一个文件上，这样在管理上不是更好吗？同理，如果将路由分功能写在不同的文件上，这样管理路由会不会比写在一个文件上好？&lt;/p&gt;</description>
      <author>R_Rboys</author>
      <pubDate>Mon, 24 Sep 2012 10:33:36 +0800</pubDate>
      <link>https://ruby-china.org/topics/5714</link>
      <guid>https://ruby-china.org/topics/5714</guid>
    </item>
    <item>
      <title>请教 gbk 字符的问题</title>
      <description>&lt;p&gt;我遇到了这样的一串字符串：a = "\xE4\xBD\xA0"
 puts a                                   #=&amp;gt; "你"
 puts a.length                         #=&amp;gt; 3
 p a                                       #=&amp;gt; "\xE4\xBD\xA0"
 p a.length                             #=&amp;gt; 3
然后让 a 和 字符'你'进行比较
 a.eql? ‘你'                             #=&amp;gt; false&lt;/p&gt;

&lt;p&gt;请问怎样才能正确的比较 a 和字符 '你'&lt;/p&gt;

&lt;p&gt;补充：a.encoding #=&amp;gt; &lt;a rel="nofollow" target="_blank"&gt;Encoding:ASCII-8BIT&lt;/a&gt;&lt;/p&gt;</description>
      <author>R_Rboys</author>
      <pubDate>Thu, 13 Sep 2012 17:43:14 +0800</pubDate>
      <link>https://ruby-china.org/topics/5529</link>
      <guid>https://ruby-china.org/topics/5529</guid>
    </item>
    <item>
      <title>有什么好的书籍，麻烦推荐下！</title>
      <description>&lt;p&gt;前几天刚看完《Ruby 元编程》，现在又想找些书看，请问有什么好的书籍，麻烦推荐下！&lt;/p&gt;

&lt;p&gt;我看过的书有：《Web 开发敏捷之道》
                     《Programming Ruby》
                     《代码整洁之道》
                     《Ruby 元编程》&lt;/p&gt;</description>
      <author>R_Rboys</author>
      <pubDate>Tue, 28 Aug 2012 11:50:42 +0800</pubDate>
      <link>https://ruby-china.org/topics/5202</link>
      <guid>https://ruby-china.org/topics/5202</guid>
    </item>
  </channel>
</rss>
