<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>lehug (changsheng)</title>
    <link>https://ruby-china.org/lehug</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>over</title>
      <description>&lt;p&gt;over&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Sat, 08 Apr 2023 16:05:49 +0800</pubDate>
      <link>https://ruby-china.org/topics/42990</link>
      <guid>https://ruby-china.org/topics/42990</guid>
    </item>
    <item>
      <title>db/schema.rb 数据库迁移后的数据库模式文件需要加入到版本控制中吗？</title>
      <description>&lt;p&gt;在实际项目，团队对于是否把 schema.rb 加入到版本控制中做了一些讨论。想延伸下，听听各位的意见。&lt;/p&gt;

&lt;p&gt;目前我们把其加入到了版本控制，带来了一些问题： &lt;code&gt;经常冲突，然后解决冲突，存在解决冲突不佳导致的副作用&lt;/code&gt;。
同时发现不加入版本控制，交给 migrate 文件自行构建，暂未发现副作用。并且大部分 github 项目都将 schema.rb 加入到了.gitignore。&lt;/p&gt;

&lt;p&gt;但是 rails guide 或者 rails 风格指引中都提到建议加到版本控制&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;6.3 数据库模式转储和源码版本控制
数据库模式转储是数据库模式的可信来源，因此强烈建议将其纳入源码版本控制。

db/schema.rb 文件包含数据库的当前版本号，这样可以确保在合并两个包含数据库模式文件的分支时会发生冲突。一旦出现这种情况，就需要手动解决冲突，保留版本较高的那个数据库模式文件。
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;其中提到了一个好处是&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;当需要部署 Rails 应用的新实例时，不必把所有迁移重新运行一遍，直接加载当前数据库的模式文件要简单和快速得多。
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;但是我们实际项目中有 200+ 的表，空库执行 rails db:migrate 耗时也不超过一分钟（甚至更少），所以我觉得此好处可忽略。&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;你们将其加入到版本控制了吗？不加入版本控制后，你们遇到了什么副作用？&lt;/strong&gt;&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Sat, 04 Nov 2017 11:12:43 +0800</pubDate>
      <link>https://ruby-china.org/topics/34510</link>
      <guid>https://ruby-china.org/topics/34510</guid>
    </item>
    <item>
      <title>ActiveRecord Transactions 官方文档内容提炼与总结</title>
      <description>&lt;p&gt;内容来自 &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html" rel="nofollow" target="_blank" title=""&gt;rails 官方&lt;/a&gt;,可直接观看。&lt;/p&gt;

&lt;p&gt;提炼出一下知识点：&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;事务是基于数据库连接，不是基于模型的 (transactions are per-database connection, not per-model)&lt;/li&gt;
&lt;li&gt;可以基于模型的类 model class 或者模型的实例 model instance，或者用 ActiveRecord::Base&lt;/li&gt;
&lt;li&gt;基于上面两点，事务的编写可以不用 ActiveRecord::Base，而是根据当前的代码，拿当前模型的类或者实例即可。&lt;/li&gt;
&lt;li&gt;事务不会跨数据库连接传播 (Transactions are not distributed across database connections)&lt;/li&gt;
&lt;li&gt;save,destroy 自动的嵌套了事务，所有的 callbacks 在事务中，包括 after_*的回调。所以 after_save 后抛异常，也会回滚&lt;/li&gt;
&lt;li&gt;异常

&lt;ul&gt;
&lt;li&gt; ActiveRecord::StatementInvalid 此异常抛出后，数据库连接已经断开，以后的数据库操作都将无法进行&lt;/li&gt;
&lt;li&gt;ActiveRecord::Rollback 会被当前事务捕获，触发一次回滚，但是不会被再次抛出异常&lt;/li&gt;
&lt;li&gt;其他的异常抛出后，会触发回滚&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;嵌套事务

&lt;ul&gt;
&lt;li&gt;默认使用最外层的那个事务&lt;/li&gt;
&lt;li&gt;除 ms-sql 外，其他数据库并不支持真正的嵌套事务，用 savepoint 来实现的&lt;/li&gt;
&lt;li&gt;嵌套事务中，假如不指定 requires_new: true，那么嵌套部分事务，是最外层事务的一部分，并不是一个真正的事务&lt;/li&gt;
&lt;li&gt;ActiveRecord::Rollback 在嵌套事务中的效果:

&lt;ul&gt;
&lt;li&gt;没有 requires_new: true，那么当前事务 (非真正的事务) 捕获，并且不会再次抛出异常，则外层的真正事务会执行，并且嵌套部分也没有回滚，而是被数据库保存。&lt;/li&gt;
&lt;li&gt;requires_new: true 后，当前事务会回滚，最外层事务在没有其他异常时不会回滚。&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;非 ActiveRecord::Rollback 的异常，无论是否在嵌套内，都会被传播，最终引发回滚&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;</description>
      <author>lehug</author>
      <pubDate>Tue, 31 Oct 2017 09:04:24 +0800</pubDate>
      <link>https://ruby-china.org/topics/34474</link>
      <guid>https://ruby-china.org/topics/34474</guid>
    </item>
    <item>
      <title>Rails 5 + ransack + PosgreSQL 怎么借用 ransack 搜索 array？</title>
      <description>&lt;p&gt;问题如上，找到一个解决方案是在 rails4 下，用 postgres_ext 这个 gem，但是这个 gem 不支持 rails5。没有看到 ransack 支持这一块的信息，
我期望的效果是类似 in 的效果。请教各位有何种解决方案？&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Fri, 25 Aug 2017 09:18:21 +0800</pubDate>
      <link>https://ruby-china.org/topics/33935</link>
      <guid>https://ruby-china.org/topics/33935</guid>
    </item>
    <item>
      <title>不用 eval 的情况下，怎样将字符串转成 array 或者 hash</title>
      <description>&lt;p&gt;前端有一个时间控件，选择一个时间段，传入后端为一个数组，长度为 2，正常的 controller 可以将这个参数正常解析为数组。
但是我借用 ransack 然后在 config/initializers/ransack.rb 中定义如下方法：&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ransack.configure do |config|
  config.add_predicate 'between',
    arel_predicate: 'between',
    formatter: proc { |v|
    parts = eval(v) 
      OpenStruct.new(begin: parts['0'].to_time, end: parts['1'].to_time)
    },
    validator: proc { |v| v.present? },
    type: :string
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;其中 formatter 中拿到的 v，内容类似： '{"0"=&amp;gt;"Mon Jun 26 2017 16:31:43 GMT+0800", "1"=&amp;gt;"Mon Jul 31 2017 16:31:43 GMT+0800"}'，是一个 string 类型。&lt;/p&gt;

&lt;p&gt;如上 eval 处理可以将其解析为 hash，请问 eval 外有方法解析这个字符串吗？&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Wed, 28 Jun 2017 17:14:19 +0800</pubDate>
      <link>https://ruby-china.org/topics/33348</link>
      <guid>https://ruby-china.org/topics/33348</guid>
    </item>
    <item>
      <title>工具求助 atom 下如何实现跳转到声明方法 go to declaration?</title>
      <description>&lt;p&gt;现在开始由 rubymine 转到 atom，安装了一些插件，但是终究还是没有搞定 go to declaration 的问题。选中后右键，有一个 go to declaration，但是不执行。是不是 ruby 环境依赖一些新的组件？求助各位，如何配置可以实现 go to declaration，谢谢~&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Thu, 21 Jul 2016 10:09:50 +0800</pubDate>
      <link>https://ruby-china.org/topics/30583</link>
      <guid>https://ruby-china.org/topics/30583</guid>
    </item>
    <item>
      <title>如何跳过 after_save 等回调方法？</title>
      <description>&lt;p&gt;在我代码初始化数据的时候，我不希望他执行回调，因为这样执行效率比较慢，我希望初始化完毕后，统一执行以下回调。但是查阅了一些 rails 跳过回调的文档，提供的方法 mongodb 没法识别，mongodb 官方文档也没有跳过回调的说明，求助大神们，怎么操作可以跳过回调？&lt;/p&gt;

&lt;p&gt;项目是 rails4.6+mongodb&lt;/p&gt;

&lt;p&gt;我参考过的文章：
&lt;a href="https://docs.mongodb.com/ecosystem/tutorial/mongoid-callbacks/" rel="nofollow" target="_blank"&gt;https://docs.mongodb.com/ecosystem/tutorial/mongoid-callbacks/&lt;/a&gt;
&lt;a href="http://guides.ruby-china.org/active_record_callbacks.html" rel="nofollow" target="_blank"&gt;http://guides.ruby-china.org/active_record_callbacks.html&lt;/a&gt;&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Sun, 10 Jul 2016 15:43:14 +0800</pubDate>
      <link>https://ruby-china.org/topics/30489</link>
      <guid>https://ruby-china.org/topics/30489</guid>
    </item>
    <item>
      <title>CoffeeScript 中当前对象怎么获取？</title>
      <description>&lt;p&gt;比如一个 checkbox 列表，然后我通过 class 属性对所有的 checkbox 绑定了事件，在事件中，如何写才能获得到当前点击行的 checkbox 的其他属性？普通的 js 用$(this) 就可以了，但是我在 coffeeScript 中，使用 self,@,$(this) 都不可以，求助究竟应该用什么，谢谢&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Sun, 26 Jun 2016 19:02:16 +0800</pubDate>
      <link>https://ruby-china.org/topics/30370</link>
      <guid>https://ruby-china.org/topics/30370</guid>
    </item>
    <item>
      <title>用 grape 和 swagger，如何使得生成的文档中包含两个实例？</title>
      <description>&lt;p&gt;在用 grape，配套用了 swagger 用于生成文档，当前 desc 的 success 中写两个 entities 实例，结果生成的页面只显示最后一个，如何操作可以使得 success 中包含两个实例并生成到帮助文档中，方便别人查阅？&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Fri, 06 May 2016 18:00:24 +0800</pubDate>
      <link>https://ruby-china.org/topics/29937</link>
      <guid>https://ruby-china.org/topics/29937</guid>
    </item>
    <item>
      <title>如何 create 使得 after_save 回调不执行</title>
      <description>&lt;p&gt;环境是 rails+mongodb。现在有一个关联表，初始化的时候，调用的是 find_or_create_by，并且 model 中定义了 after_save 为了数据修改后执行一个函数，结果初始化 find_or_create_by 的时候就会调用 after_save 使得运行很慢，如何操作可以让初始化 create 的时候不执行 after_save?&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Wed, 20 Apr 2016 08:47:24 +0800</pubDate>
      <link>https://ruby-china.org/topics/29782</link>
      <guid>https://ruby-china.org/topics/29782</guid>
    </item>
    <item>
      <title>求推荐 lazy load 惰性延迟的一些思路</title>
      <description>&lt;p&gt;要做一个电商首页，里面会有大量的图片与商品附属信息（如价格，标题等），打算基于用户体验考虑，楼层采用 lazy load 方式按需加载，没有做过这种项目，请问下以什么样的思路实现比较好？&lt;/p&gt;

&lt;p&gt;我尝试去寻找了一些开源的 js 库，但是搜到的大多是图片延迟加载的，大概思路是原 img 的 src 中统一加载一个文件，然后真实加载的图片地址放在 data 属性中，需要时再赋值到 src 中请求服务器。但是我需要的不仅仅是图片，而是包含 div 样式，图片，价格，标题等信息等一串 html 文件。&lt;/p&gt;

&lt;p&gt;基于我的需求，有成熟的插件可以使用帮我完成一部分东西吗？还是说完全需要我自己根据窗口位置或者鼠标移入等借用 ajax 获取数据并填充到对应位置？&lt;/p&gt;

&lt;p&gt;求有经验的人士帮忙提供下思路，谢谢。&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Sat, 26 Mar 2016 08:18:27 +0800</pubDate>
      <link>https://ruby-china.org/topics/29466</link>
      <guid>https://ruby-china.org/topics/29466</guid>
    </item>
    <item>
      <title>MongoDB 下如何模糊查询字段以 XXX 开头的数据？</title>
      <description>&lt;p&gt;mongodb 下如何模糊查询字段以 XXX 开头的数据？我知道 Table.where(:tag=&amp;gt;/#{tag}/) 这个可以，但是有没有更精确的按照开头匹配的语句写法？&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Thu, 24 Mar 2016 14:56:29 +0800</pubDate>
      <link>https://ruby-china.org/topics/29440</link>
      <guid>https://ruby-china.org/topics/29440</guid>
    </item>
    <item>
      <title>form_for 下面的 select 怎么定义 class 属性？</title>
      <description>&lt;p&gt;form_for 下面有一个 select 多选框，&amp;lt;%=f.select(:page,options_for_select([['首页','sy']]))%&amp;gt;，求教这个里面如何添加 class 属性？
源码是这样的，有些看不懂
def select(method, choices = nil, options = {}, html_options = {}, &amp;amp;block)
        &lt;a href="/template.select" class="user-mention" title="@template.select"&gt;&lt;i&gt;@&lt;/i&gt;template.select&lt;/a&gt;(&lt;a href="/object_name" class="user-mention" title="@object_name"&gt;&lt;i&gt;@&lt;/i&gt;object_name&lt;/a&gt;, method, choices, objectify_options(options), &lt;a href="/default_options.merg" class="user-mention" title="@default_options.merg"&gt;&lt;i&gt;@&lt;/i&gt;default_options.merg&lt;/a&gt;e(html_options), &amp;amp;block)
      end
麻烦大神给个示范，谢谢&lt;/p&gt;</description>
      <author>lehug</author>
      <pubDate>Sat, 12 Mar 2016 13:58:34 +0800</pubDate>
      <link>https://ruby-china.org/topics/29276</link>
      <guid>https://ruby-china.org/topics/29276</guid>
    </item>
  </channel>
</rss>
