On Thu, Sep 23, 2021 at 12:40 PM 'Matt Culpepper' via sequel-talk <
sequel-talk@googlegroups.com> wrote:

> This code:
>
> Model.association_join(:another_model).each { |m| puts m.id }
>
> is outputting the id for `another_model` rather than the original model
> id. That's very unexpected to me. Is this intentional?
>

Yes.  association_join only does the join part, so it is equivalent to:

SELECT * FROM table LEFT JOIN other_table ON (conditions)

The way Sequel's database adapters work, later column values will overwrite
earlier column values if the dataset returns multiple columns with the same
name.

If any of the column names in the tables overlap, you are likely to have
problems.  Use an explicit selection:

  Model.association_join(:another_model).select_all(Model.table_name).each
{ |m| puts m.id }

or use eager_graph instead of association_join, which sets up appropriate
column aliases (if doing this, you need to switch from #each to #all).

An alternative approach is using the table_select plugin, which changes the
default selection for the model from * to table.*.

Thanks,
Jeremy

-- 
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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSfzKd_0Fk-p-bAfQDbLhAi%2B-R59av6PpaGs8psEwf9YNg%40mail.gmail.com.

Reply via email to