Sequelのassociation_join時に異なるテーブル・同じカラム名の値が上書きされる

検証バージョン: 4.32.0 acdc2ed

TL;DR
-----

Sequelのassociation_joinのお話

ParentとChildとGrandChildという3つのテーブルにおいて、例えばcreated_atという同じカラムがあった場合にassociation_joinをすると、返ってきたオブジェクトのカラムの値じゃないよ、って話

```rb
Parent.first.created_at
=> 2016-03-09 09:11:25 +0900
GrandChild.first.created_at
=> 2016-03-09 09:11:30 +0900

r = Parent.association_join(:child => :grand_child).first
r.class
=> Parent
r.created_at
=> 2016-03-09 09:11:30 +0900 ## OMG! this is GrandChild's value
```

Workaround
----------

DataSetを作る前(=SQLが実行される前)にAliasして、逃げる方法があります

```rb
Parent.first.created_at
=> 2016-03-09 09:11:25 +0900
GrandChild.first.created_at
=> 2016-03-09 09:11:30 +0900

r = Parent.select_append(Sequel.as(Sequel.qualify(:parents, :created_at), :hoge)).association_join(:child => :grand_child).first
r.class
=> Parent
r.created_at
=> 2016-03-09 09:11:30 +0900
r[:hoge]
=> 2016-03-09 09:11:25 +0900 ## Great! (but cannot use #hoge ...)
```

検証コード
--------

### コード ###

```rb:sequel_association_join_value_of_same_column.rb
require "sequel"
DB = Sequel.sqlite

DB.create_table :grand_children do
  primary_key :id
  Integer :child_id
  String  :name
  DateTime :created_at
end
DB.create_table :children do
  primary_key :id
  Integer :parent_id
  String  :name
  DateTime :created_at
end
DB.create_table :parents do
  primary_key :id
  String :name
  DateTime :created_at
end

class GrandChild < Sequel::Model
  plugin :timestamps
  many_to_one :child
end
class Child < Sequel::Model
  plugin :timestamps
  many_to_one :parent
  one_to_many :grand_child
end
class Parent < Sequel::Model
  plugin :timestamps
  one_to_many :child
end

## main

# Create data
p1 = Parent.create(name: "p1") ; sleep 2
c1 = p1.add_child(name: "c1")  ; sleep 2
_ = c1.add_grand_child(name: "g1")

# Inspect
r1 = Parent.association_join(:child => :grand_child).first
r2 = GrandChild.association_join(:child => :parent).first

puts <<-EOT
## created_at of each table

    Parent: #{Parent.first.created_at}
     Child: #{Child.first.created_at}
GrandChild: #{GrandChild.first.created_at}

## check joined created_at

Lookup path(top-down) : Parent => Child => GrandChild
Return class is #{r1.class}
created_at: #{r1.created_at}
(#{r1.class}'s created_at is #{r1.class.first.created_at})

Lookup path(bottom-up): Parent <= Child <= GrandChild
Return class is #{r2.class}
created_at: #{r2.created_at}
(#{r2.class}'s created_at is #{r2.class.first.created_at})

EOT

r = Parent.select_append(Sequel.as(Sequel.qualify(:parents, :created_at), :hoge)).association_join(:child => :grand_child).first

puts <<-EOT
## workaround

Return class is #{r.class}
created_at: #{r.created_at}
      hoge: #{r[:hoge]}
(#{r.class}'s created_at is #{r.class.first.created_at})

EOT
```

### 実行結果 ###

```
$ bundle exec ruby sequel_association_join_value_of_same_column.rb 
## created_at of each table

    Parent: 2016-03-09 09:14:18 +0900
     Child: 2016-03-09 09:14:20 +0900
GrandChild: 2016-03-09 09:14:22 +0900

## check joined created_at

Lookup path(top-down) : Parent => Child => GrandChild
Return class is Parent
created_at: 2016-03-09 09:14:22 +0900
(Parent's created_at is 2016-03-09 09:14:18 +0900)

Lookup path(bottom-up): Parent <= Child <= GrandChild
Return class is GrandChild
created_at: 2016-03-09 09:14:18 +0900
(GrandChild's created_at is 2016-03-09 09:14:22 +0900)

## workaround

Return class is Parent
created_at: 2016-03-09 09:14:22 +0900
      hoge: 2016-03-09 09:14:18 +0900
(Parent's created_at is 2016-03-09 09:14:18 +0900)

```

あとがき
-------

そのうちIssue出しておきます