<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Amosasas (AmosChen)</title>
    <link>https://ruby-china.org/Amosasas</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>如何设置 rails console 中 app.host 的默认值？</title>
      <description>&lt;p&gt;在&lt;code&gt;Rails 6.0&lt;/code&gt;中：&lt;/p&gt;

&lt;p&gt;在&lt;code&gt;rails console -e development&lt;/code&gt;或者&lt;code&gt;rails console -e test&lt;/code&gt;想进行一些简单的调试，但是&lt;code&gt;app.get&lt;/code&gt;一直是 403，查看 app.host 发现是 www.example.com，设置成 localhost 就行了，但是每次都要这样搞一遍很麻烦，这个东西可以用啥配置？我自己尝试往&lt;code&gt;config/environments/xx.rb&lt;/code&gt;里加 config，但是&lt;a href="https://guides.rubyonrails.org/configuring.html" rel="nofollow" target="_blank"&gt;https://guides.rubyonrails.org/configuring.html&lt;/a&gt;中并没有找到合适的。有大佬知道这个东西应该怎么设置吗？&lt;/p&gt;</description>
      <author>Amosasas</author>
      <pubDate>Thu, 23 Apr 2020 15:19:55 +0800</pubDate>
      <link>https://ruby-china.org/topics/39780</link>
      <guid>https://ruby-china.org/topics/39780</guid>
    </item>
    <item>
      <title>Rails 中关于:scope 的设计问题</title>
      <description>&lt;p&gt;假设有个 table 叫做 People，有个属性叫做 sex，类型是 string，还有个属性叫 country，类型也是 string
有个 controller 中要做这样的事情：
&lt;code&gt;app/controller/person_controller.rb&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PersonController &amp;lt; ApplicationController
  def some_method
    sex = params[:sex] # sex可以是nil
    country = params[:country] # country可以是nil
    # 返回指定sex country的记录 是nil不限制
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;我现在想到一种方法是用 scope（根据大佬提醒已经修改）
&lt;code&gt;app/models/person.rb&lt;/code&gt;&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person &amp;lt; ApplicationRecord
  # 省略一些validate
  scope :by_sex, -&amp;gt;(sex) { where(sex: sex) if sex.present? }
  scope :by_country, -&amp;gt;(country) { where(country: country) if country.present? }
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;现在的问题是，怎么改动支持一种 by_sex(!"male") 查询不是 male 的方法？&lt;/p&gt;

&lt;p&gt;·&lt;code&gt;使用where.not就行了 是我想多了 hmm&lt;/code&gt;&lt;/p&gt;</description>
      <author>Amosasas</author>
      <pubDate>Sat, 07 Mar 2020 20:25:34 +0800</pubDate>
      <link>https://ruby-china.org/topics/39572</link>
      <guid>https://ruby-china.org/topics/39572</guid>
    </item>
  </channel>
</rss>
