<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <generator>Persumi - Level up your writing and blogging with AI</generator>
  <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
  <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/gotcha"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/gotcha/feed/rss"/>
  <link rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/gotcha/feed/atom"/>
  <author>
    <name>Fred Wu</name>
    <email>ifredwu@gmail.com</email>
    <uri>http://persumi.com/u/fredwu</uri>
  </author>
  <subtitle/>
  <id>http://persumi.com/u/fredwu/tech/e/blog/t/gotcha</id>
  <title>Blog (gotcha) - Fred Wu&apos;s Tech</title>
  <updated>2026-06-21T03:28:41.431327Z</updated>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;
I haven’t really used &lt;a href=&quot;http://sequel.rubyforge.org/&quot;&gt;Sequel&lt;/a&gt; much therefore I am definitely a newbie. However, after days and nights of frustration, endless debugging and some search-fu during the development of &lt;a href=&quot;https://github.com/fredwu/datamappify&quot;&gt;Datamappify&lt;/a&gt;, I have finally arrived at the conclusion that Sequel is a capable library, as long as you are aware of the gotchas.&lt;/p&gt;
&lt;h3&gt;
Gotcha 1: Always use “&lt;code class=&quot;inline&quot;&gt;select&lt;/code&gt;“/“&lt;code class=&quot;inline&quot;&gt;select_all&lt;/code&gt;“, or your data records will mysteriously have wrong IDs!&lt;/h3&gt;
&lt;p&gt;
In ActiveRecord, joining an associated model couldn’t be simpler:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.joins(:author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
In Sequel, despite having a similar API for models to declare associations and their corresponding primary and foreign keys, you cannot do a &lt;code class=&quot;inline&quot;&gt;join&lt;/code&gt; without specifying the keys:&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Not good:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.join(:authors)
# or
Post.join(Author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;em&gt;Better:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.join(:authors, :id =&gt; :author_id)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
You would think the version above works - it doesn’t. Even worse, the above example &lt;strong&gt;will give you incorrect data&lt;/strong&gt; - the IDs of the Post records will now contain the IDs from their corresponding Author records! This is because upon a &lt;code class=&quot;inline&quot;&gt;join&lt;/code&gt;, Sequel merges attributes from both models into a single hash.&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;The correct version:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.join(:authors, :id =&gt; :author_id).select(:posts __id, :posts__ title, :posts__body)
# or
Post.join(:authors, :id =&gt; :author_id).select_all(:posts)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_inline_mrvsa2sBB81qz4rgp.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
&lt;h3&gt;
Gotcha 2: Always call “&lt;code class=&quot;inline&quot;&gt;all&lt;/code&gt;“ at the end of the chain, or the chain will present data in a different format.&lt;/h3&gt;
&lt;p&gt;
In ActiveRecord, all of the below examples return an &lt;code class=&quot;inline&quot;&gt;ActiveRecord::Relation&lt;/code&gt; collection:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;)
Post.joins(:author)
Post.includes(:author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
And indeed, calling &lt;code class=&quot;inline&quot;&gt;first&lt;/code&gt; on any of them returns an object of class &lt;code class=&quot;inline&quot;&gt;Post&lt;/code&gt; (assuming the result collection is not empty).&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;).first.class #=&gt; Post
Post.joins(:author).first.class #=&gt; Post
Post.includes(:author).first.class #=&gt; Post&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
In Sequel, the below examples all return a &lt;code class=&quot;inline&quot;&gt;Sequel::DataSet&lt;/code&gt; collection:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;)
Post.eager(:author)
Post.eager_graph(:author)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
But let’s see what we get from calling &lt;code class=&quot;inline&quot;&gt;first.class&lt;/code&gt; on them:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;).first.class #=&gt; Post
Post.eager(:author).first.class #=&gt; Post
Post.eager_graph(:author).first.class #=&gt; Hash&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
Huh? Last one is a &lt;code class=&quot;inline&quot;&gt;Hash&lt;/code&gt;? It turns out, if you call &lt;code class=&quot;inline&quot;&gt;all&lt;/code&gt; at the end of chains to convert them to &lt;code class=&quot;inline&quot;&gt;Array&lt;/code&gt;s, then the returned collections are consistent:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot;&gt;Post.where(:title =&gt; &apos;Hello world&apos;).all.first.class #=&gt; Post
Post.eager(:author).all.first.class #=&gt; Post
Post.eager_graph(:author).all.first.class #=&gt; Post&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_inline_mrvsa2sBB81qz4rgp.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
]]&gt;</content>
    <published>2013-08-21T12:39:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/gotchas-in-the-ruby-sequel-gem"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/gotchas-in-the-ruby-sequel-gem</id>
    <title>Gotchas in the Ruby Sequel Gem</title>
    <updated>2013-08-21T12:39:00.000000Z</updated>
  </entry>
</feed>