<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>ringokun</title>
    <link>https://ruby-china.org/ringokun</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>多态情况下关联表查询问题</title>
      <description>&lt;p&gt;模型&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Image
    belongs_to :item, polymorphic: true
end

class Book
    has_many covers, class_name: "Image", as: :item
    enum status: {tech: 0, novel: 1}
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;现在我想查询 &lt;strong&gt;所属书籍的 status 为 tech 的所有图片&lt;/strong&gt;,应该怎么写呢？&lt;/p&gt;

&lt;p&gt;(因为还需要对结果进行分页处理，所以 select 不是很方便)&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;最先想的是&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Image.includes(:item).where(items: {status: 0})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;然而这样并不行。。。应该是多态的原因。&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;在&lt;a href="https://ruby-china.org/topics/21537" title=""&gt;论坛&lt;/a&gt;上找到了一个解决方案：&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Image
    belongs_to :item, polymorphic: true
    belongs_to :book, foreign_key: item_id, foreigen_type: book
end
&lt;/code&gt;&lt;/pre&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Image.includes(:book).where(books: {status: 0})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;这样是可行的，我的理解是这个方法免去了多态的一环，让 Image 直接和 Book 关联起来，从而可以直接使用常规方法查询。&lt;/p&gt;

&lt;p&gt;不过还想问一下有没有其他更好的办法&lt;/p&gt;

&lt;p&gt;论坛里相似的问题&lt;a href="https://ruby-china.org/topics/21537" rel="nofollow" target="_blank"&gt;https://ruby-china.org/topics/21537&lt;/a&gt;&lt;/p&gt;</description>
      <author>ringokun</author>
      <pubDate>Fri, 19 May 2017 17:37:33 +0800</pubDate>
      <link>https://ruby-china.org/topics/33025</link>
      <guid>https://ruby-china.org/topics/33025</guid>
    </item>
    <item>
      <title>通过 OAuth 连接 Twitter 访问 /auth/twitter 显示 timeout</title>
      <description>&lt;p&gt;最近在学习 oauth，照着&lt;a href="https://www.youtube.com/watch?v=KOnRf1K6Eg8&amp;amp;t=189s" rel="nofollow" target="_blank" title=""&gt;Ruby on Rails - Railscasts PRO #241 Simple OmniAuth (revised)&lt;/a&gt;做，然后到访问&lt;a href="http://localhost:3000/auth/twitter" rel="nofollow" target="_blank"&gt;http://localhost:3000/auth/twitter&lt;/a&gt;这一步出现错误 &lt;code&gt;ERROR -- omniauth: (twitter) Authentication failure! timeout: Net::OpenTimeout, execution expired&lt;/code&gt;
&lt;/p&gt;&lt;p&gt;Google 后没找到合适的答案，觉得是不是因为被墙了，但是开了全局代理还是不行，请问这个应该如何解决呢？&lt;/p&gt;&lt;p&gt;omniauth.rb 里面是按照官方文档写的&lt;code&gt;provider :twitter, "API_KEY", "API_SECRET"&lt;/code&gt; 申请 twitter app 的时候 Callback URL 写的是&lt;code&gt;http://localhost:3000/auth/twitter/callback&lt;/code&gt;&lt;/p&gt;</description>
      <author>ringokun</author>
      <pubDate>Fri, 27 Jan 2017 21:40:58 +0800</pubDate>
      <link>https://ruby-china.org/topics/32217</link>
      <guid>https://ruby-china.org/topics/32217</guid>
    </item>
  </channel>
</rss>
