<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>I Computer</title>
    <description></description>
    <link>https://booox.github.io/</link>
    <atom:link href="https://booox.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 02 Jun 2019 13:52:56 +0000</pubDate>
    <lastBuildDate>Sun, 02 Jun 2019 13:52:56 +0000</lastBuildDate>
    <generator>Jekyll v3.8.5</generator>
    
      <item>
        <title>Rails 5 使用 Migration 来操作数据库</title>
        <description>
Rails 5 使用 Migration 来操作数据库

</description>
        <pubDate>Sun, 02 Jun 2019 08:21:37 +0000</pubDate>
        <link>https://booox.github.io/2019/06/02/rails-migration/</link>
        <guid isPermaLink="true">https://booox.github.io/2019/06/02/rails-migration/</guid>
        
        <category>rails</category>
        
        <category>migration</category>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>Rails 5 使用 `rake db:seed` 添加测试数据 </title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#添加单条数据&quot; id=&quot;markdown-toc-添加单条数据&quot;&gt;添加单条数据&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#添加多条数据&quot; id=&quot;markdown-toc-添加多条数据&quot;&gt;添加多条数据&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#从-csv-文件中添加数据&quot; id=&quot;markdown-toc-从-csv-文件中添加数据&quot;&gt;从 csv 文件中添加数据&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#使用-faker-添加测试数据&quot; id=&quot;markdown-toc-使用-faker-添加测试数据&quot;&gt;使用 Faker 添加测试数据&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#links&quot; id=&quot;markdown-toc-links&quot;&gt;Links&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

Rails 5 使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;rake db:seed&lt;/code&gt; 添加测试数据

在 Rails 的开发中，经常会使用一些测试数据，可以使用方法有：

&lt;ul&gt;
  &lt;li&gt;在浏览器端手工添加&lt;/li&gt;
  &lt;li&gt;在 &lt;code class=&quot;highlighter-rouge&quot;&gt;rails console&lt;/code&gt; 中手工添加&lt;/li&gt;
  &lt;li&gt;通过访问数据库来手工添加数据&lt;/li&gt;
&lt;/ul&gt;

如果只是很少的数据，这样还能接受。但如果数据量比较大，就非常麻烦，还有一个问题就是数据库被清掉之后，又得重新来过，此时比较方便的方法是，使用 &lt;code class=&quot;highlighter-rouge&quot;&gt;rake db:seed&lt;/code&gt; 来自动添加测试的数据。

总体思路：

&lt;ul&gt;
  &lt;li&gt;修改 &lt;em&gt;db/seeds.rb&lt;/em&gt; 文件，添加测试数据的代码&lt;/li&gt;
  &lt;li&gt;执行 &lt;code class=&quot;highlighter-rouge&quot;&gt;$ rake db:seed&lt;/code&gt;，完成数据添加&lt;/li&gt;
&lt;/ul&gt;

下面通过具体的例子来说明。

&lt;h2 id=&quot;添加单条数据&quot;&gt;添加单条数据&lt;/h2&gt;

&lt;strong&gt;任务要求&lt;/strong&gt; 添加一个新的 Group

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# db/seeds.rb
Group.create(title: 'abc', description: 'abc123')
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果需要登录后才能创建 Group，即与用户关联起来，可以这样：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# db/seeds.rb
Group.create(title: 'abc', description: 'abc123', user_id: 1)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行 &lt;code class=&quot;highlighter-rouge&quot;&gt;$ rake db:seed&lt;/code&gt;

&lt;h2 id=&quot;添加多条数据&quot;&gt;添加多条数据&lt;/h2&gt;

&lt;strong&gt;任务要求&lt;/strong&gt; 添加 3 个 Group

即传入一个列表，每条数据为一个字典。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# db/seeds.rb
Group.create([{ title: 'b1', description: 'b11', user_id: 1 },
              { title: 'b2', description: 'b22', user_id: 1 },
              { title: 'b3', description: 'b33', user_id: 1 }])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行 &lt;code class=&quot;highlighter-rouge&quot;&gt;$ rake db:seed&lt;/code&gt;

&lt;strong&gt;任务要求&lt;/strong&gt; 添加 50 个 Group

使用循环来实现。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# db/seeds.rb
50.times do |number|
  Group.create(title: &quot;title-#{number}&quot;, description: &quot;des-#{number}&quot;, user_id: 1)
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行 &lt;code class=&quot;highlighter-rouge&quot;&gt;$ rake db:seed&lt;/code&gt;

&lt;h2 id=&quot;从-csv-文件中添加数据&quot;&gt;从 csv 文件中添加数据&lt;/h2&gt;

&lt;strong&gt;任务要求&lt;/strong&gt; 通过 CSV 文件添加 Group

读取 csv 文件，使用循环添加数据。

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# db/seeds.rb
require &quot;csv&quot;

CSV.foreach('db/groups.csv') do |group|
  Group.create(title: group[0], description: group[1], user_id: 1)
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行 &lt;code class=&quot;highlighter-rouge&quot;&gt;$ rake db:seed&lt;/code&gt;

&lt;h2 id=&quot;使用-faker-添加测试数据&quot;&gt;使用 Faker 添加测试数据&lt;/h2&gt;

Faker 是一个产生测试数据的 Gem.
需要修改 Gemfile 如下：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;group :development do
  # ...
  gem 'faker'
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行：

&lt;code class=&quot;highlighter-rouge&quot;&gt;$ bundle install&lt;/code&gt;

再次编辑 &lt;em&gt;db/seeds.rb&lt;/em&gt; 文件如下：

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# db/seeds.rb
require 'faker'

50.times do |group|
  Group.create(title: Faker::Company.name, description: Faker::Company.catch_phrase, user_id: 1)
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行 &lt;code class=&quot;highlighter-rouge&quot;&gt;$ rake db:seed&lt;/code&gt;

关于 Faker 的用法，可以查看下面的帮助文档。

&lt;h2 id=&quot;links&quot;&gt;Links&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/stympy/faker&quot;&gt;stympy - faker&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sun, 05 May 2019 20:04:43 +0000</pubDate>
        <link>https://booox.github.io/2019/05/05/rails-db-seeds/</link>
        <guid isPermaLink="true">https://booox.github.io/2019/05/05/rails-db-seeds/</guid>
        
        <category>rails</category>
        
        <category>seeds</category>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>Rails 5 渲染 Markdown 及实现代码高亮</title>
        <description>
Rails 5 渲染 Markdown 及实现代码高亮

</description>
        <pubDate>Tue, 07 Aug 2018 11:01:38 +0000</pubDate>
        <link>https://booox.github.io/2018/08/07/rails-markdown-syntax-highlight-with-redcarpet/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/08/07/rails-markdown-syntax-highlight-with-redcarpet/</guid>
        
        <category>rails</category>
        
        <category>redcarpet</category>
        
        <category>coderay</category>
        
        <category>rouge</category>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>Rails 5 中使用CarrierWave 在 `remote: true` 实现上传图片不成功</title>
        <description>
Rails 5 中使用CarrierWave 在 &lt;code class=&quot;highlighter-rouge&quot;&gt;remote: true&lt;/code&gt; 实现上传图片不成功

</description>
        <pubDate>Mon, 11 Jun 2018 05:53:15 +0000</pubDate>
        <link>https://booox.github.io/2018/06/11/rails-uploader-image-with-carrierwave-nameerror-uninitialized-constant/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/06/11/rails-uploader-image-with-carrierwave-nameerror-uninitialized-constant/</guid>
        
        <category>rails</category>
        
        <category>CarrierWave</category>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>Rails 5 中 accepts_nested_attributes_for 不起作用</title>
        <description>
Rails 5 中 accepts_nested_attributes_for 不起作用。

</description>
        <pubDate>Sun, 10 Jun 2018 19:08:50 +0000</pubDate>
        <link>https://booox.github.io/2018/06/10/rails-nested-attributes-not-working/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/06/10/rails-nested-attributes-not-working/</guid>
        
        <category>rails</category>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>在 Rails 中使用 Bulma　框架</title>
        <description>
通过本文可以了解到如何在 Rails 中使用 Bulma　框架。

</description>
        <pubDate>Wed, 30 May 2018 12:07:03 +0000</pubDate>
        <link>https://booox.github.io/2018/05/30/use-bulma-with-rails/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/05/30/use-bulma-with-rails/</guid>
        
        <category>rails</category>
        
        <category>bulma</category>
        
        
        <category>rails</category>
        
      </item>
    
      <item>
        <title>SQlite3  Replace 用法 (译)</title>
        <description>
通过本文可以了解到如何使用 SQlite Replace 在数据表当中插入或替换已经存在的记录。

</description>
        <pubDate>Tue, 06 Mar 2018 14:09:17 +0000</pubDate>
        <link>https://booox.github.io/2018/03/06/sqlite3-replace-statement/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/03/06/sqlite3-replace-statement/</guid>
        
        <category>sqlite3</category>
        
        
        <category>sqlite3</category>
        
      </item>
    
      <item>
        <title>SQlite3 Index 用法 (译)</title>
        <description>
通过本文可以了解到如何使用 SQlite Index 来快速查询数据，加速排序以及加强唯一性检查。

</description>
        <pubDate>Tue, 06 Mar 2018 12:23:26 +0000</pubDate>
        <link>https://booox.github.io/2018/03/06/sqlite3-index-statement/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/03/06/sqlite3-index-statement/</guid>
        
        <category>sqlite3</category>
        
        
        <category>sqlite3</category>
        
      </item>
    
      <item>
        <title>在 SQlite3 中删除重复的记录</title>
        <description>
在 SQlite3 中删除重复的记录

</description>
        <pubDate>Tue, 06 Mar 2018 11:04:34 +0000</pubDate>
        <link>https://booox.github.io/2018/03/06/sqlite3-remove-duplicate-rows/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/03/06/sqlite3-remove-duplicate-rows/</guid>
        
        <category>sqlite3</category>
        
        
        <category>sqlite3</category>
        
      </item>
    
      <item>
        <title>在 Anaconda 中用命令行安装 Python2.7.3</title>
        <description>
在 Anaconda 中用命令行安装 Python2.7.3

</description>
        <pubDate>Thu, 01 Mar 2018 09:10:44 +0000</pubDate>
        <link>https://booox.github.io/2018/03/01/anaconda-conda-create-env-python2.7/</link>
        <guid isPermaLink="true">https://booox.github.io/2018/03/01/anaconda-conda-create-env-python2.7/</guid>
        
        <category>python</category>
        
        <category>conda</category>
        
        
        <category>python</category>
        
      </item>
    
  </channel>
</rss>
