<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>kitaro000 (野酸枣)</title>
    <link>https://ruby-china.org/kitaro000</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>一个路由设计的问题</title>
      <description>&lt;p&gt;比如涉及两个资源：书、作者
我希望能从不同的路径定位到作者&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:authors&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:authors&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;这样有可能吗？&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Wed, 18 Nov 2015 15:55:46 +0800</pubDate>
      <link>https://ruby-china.org/topics/28102</link>
      <guid>https://ruby-china.org/topics/28102</guid>
    </item>
    <item>
      <title>多数据库下，能否让每个用户始终保持一个数据库的链接？</title>
      <description>&lt;p&gt;我正在做一个后台管理，需要链接多个数据库，比如 A,B,C。到底连哪个是在登录时就选好确定的。后面的操作都基于这个数据库的数据&lt;/p&gt;

&lt;p&gt;我可以在登录时这么写&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;establish_connection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:one_db&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;后来我发现，只在登录时设置是不行的，因为多个用户同时操作时，我必须在每一个 action 前都判断用户选择的是哪个库。&lt;/p&gt;

&lt;p&gt;我或许可以将用户选的 db 参数放进 session，并在 ApplicationController 中写个 before_action, 每次操作都判断并重新执行 establish_connection&lt;/p&gt;

&lt;p&gt;但我觉得这不是好办法，大家在实践中是怎么做的呢？&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Sat, 03 Oct 2015 17:52:24 +0800</pubDate>
      <link>https://ruby-china.org/topics/27547</link>
      <guid>https://ruby-china.org/topics/27547</guid>
    </item>
    <item>
      <title>多行书写的字符串，如何在输出时不换行</title>
      <description>&lt;p&gt;比如&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="sx"&gt;%Q(
  a
  b
  c
)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;输出时是“abc”&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Mon, 28 Sep 2015 13:03:14 +0800</pubDate>
      <link>https://ruby-china.org/topics/27504</link>
      <guid>https://ruby-china.org/topics/27504</guid>
    </item>
    <item>
      <title>程序启动时怎么指定 development，production，test 之外的数据库？RAILS_ENV 的参数要写什么？</title>
      <description>&lt;p&gt;database.yml 中配置好了数据源，比如其中有个 data_99，那么我在启动服务器时怎么指定链接他？
RAILS_ENV=production 会在启动程序时指定 production 数据库，这个是怎么做到的，或者说怎么自定义他？&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Tue, 15 Sep 2015 15:38:13 +0800</pubDate>
      <link>https://ruby-china.org/topics/27343</link>
      <guid>https://ruby-china.org/topics/27343</guid>
    </item>
    <item>
      <title>在 view 中，render 中的 locals 是怎么传递过去的？我想判断 partial 中是否有 locals，要怎做呢？</title>
      <description>&lt;p&gt;如题&lt;/p&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;partial: &lt;/span&gt;&lt;span class="s2"&gt;"a_view"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;locals: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;obj: &lt;/span&gt;&lt;span class="o"&gt;***&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;有的写了 locals，有的没写，我在 a_view 中要怎么判断呢？&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Sat, 12 Sep 2015 12:45:44 +0800</pubDate>
      <link>https://ruby-china.org/topics/27297</link>
      <guid>https://ruby-china.org/topics/27297</guid>
    </item>
    <item>
      <title>在 Rails 中，数据库查出的数据是怎么组装成对象的？</title>
      <description>&lt;p&gt;比如我在一个 model 中定义 driver&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:driver&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;del&gt;我在查询时就可以如下方式将 driver 字段映射到对象&lt;/del&gt;&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"models.driver AS driver"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;del&gt;model.driver&lt;/del&gt;  model[driver] 就可以调用（driver 是个字符串）
select 方法只会返回 attributes 的 hash，并不会给 &lt;code&gt;@driver&lt;/code&gt; 赋值&lt;/p&gt;

&lt;p&gt;现在 driver 不再是字符串，而是一张表，在一个较复杂的多表联查中，且在不联查 driver 表的情况下，我能拿到 driver_id
那问题来了，model 要怎么初始化 driver 呢？
我试过以下方法，但是不管用&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:driver&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;driver&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;driver_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="vi"&gt;@driver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;driver_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;各位大神支个招吧，或者告诉我 rails 组装对象那部分的源码在哪里也好啊~&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Sun, 06 Sep 2015 20:44:22 +0800</pubDate>
      <link>https://ruby-china.org/topics/27216</link>
      <guid>https://ruby-china.org/topics/27216</guid>
    </item>
    <item>
      <title>怎么再忽略年月日 的情况下比较时间？</title>
      <description>&lt;p&gt;比如我只想比较 08:20 与 09:40，而忽略 2015-04-20 这样的日期。
数据库存是的 datetime&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Wed, 15 Jul 2015 14:47:53 +0800</pubDate>
      <link>https://ruby-china.org/topics/26497</link>
      <guid>https://ruby-china.org/topics/26497</guid>
    </item>
    <item>
      <title>rails 下，如何在迭代中给属性赋值？</title>
      <description>&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;lass&lt;/span&gt; &lt;span class="no"&gt;Menu&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:children&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;children 属性是不存数据库的&lt;/p&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;parents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Menu&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
&lt;span class="n"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;p&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;children&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;parents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inspect&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;输出的东西却没有 children 属性，这是为什么？&lt;/p&gt;</description>
      <author>kitaro000</author>
      <pubDate>Fri, 08 May 2015 17:41:26 +0800</pubDate>
      <link>https://ruby-china.org/topics/25494</link>
      <guid>https://ruby-china.org/topics/25494</guid>
    </item>
  </channel>
</rss>
