Hi, sequel developer. This product is very great.

I have a question about the Model#association_join with 3 tables.

In the following code, is there any way to retrieve created_at of Parent?

```
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
```

I guess, this behavior is non-intuitive.

Currently, I have a workaround, using Alias feature.

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

Thank you.

Inspection code and result;

```
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)

```

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to