association_join overwriting id of original model with association's id

2021-09-23 Thread 'Matt Culpepper' via sequel-talk
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?

-- 

NOTICE: This message, together with 
any attachments, is intended only for the use of the individual or entity 
to which it is addressed and may contain information that is privileged, 
confidential and/or exempt from disclosure. If you are not the intended 
recipient, you are hereby notified that any use, dissemination, 
distribution, or copying of this message, or any attachment, is strictly 
prohibited. If you have received this message in error, please delete this 
message, along with any attachments, from your computer and any hardcopy. 
The contents of this communication shall not be deemed legal, tax or 
investment advice and you are advised to consult your professional 
advisors. Thank you. 

-- 
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/1a79ca86-657a-4a28-ad70-dda609b39ab1n%40googlegroups.com.


Re: association_join overwriting id of original model with association's id

2021-09-23 Thread 'Matt Culpepper' via sequel-talk
`table_select` plugin is great. You are a wizard.

On Thursday, September 23, 2021 at 4:24:45 PM UTC-5 Jeremy Evans wrote:

> On Thu, Sep 23, 2021 at 12:40 PM 'Matt Culpepper' via sequel-talk <
> seque...@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
>

-- 

NOTICE: This message, together with 
any attachments, is intended only for the use of the individual or entity 
to which it is addressed and may contain information that is privileged, 
confidential and/or exempt from disclosure. If you are not the intended 
recipient, you are hereby notified that any use, dissemination, 
distribution, or copying of this message, or any attachment, is strictly 
prohibited. If you have received this message in error, please delete this 
message, along with any attachments, from your computer and any hardcopy. 
The contents of this communication shall not be deemed legal, tax or 
investment advice and you are advised to consult your professional 
advisors. Thank you. 

-- 
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/fa923694-4f88-4204-996c-c6bc0cc612e8n%40googlegroups.com.