On Tue, Nov 23, 2021 at 8:56 AM Robert Pawlas <robspaw...@gmail.com> wrote:

> Hello,
>
> I've just tried to make many_to_one association with some additions in
> block, like in this example:
>
> class Car < Sequel::Model(DB[:default][:cars])
> many_to_one :company, class: 'Company', read_only: true, do |ds|
> ds.select_append(
> Sequel[:cars][:manufacturers].as(:producer),
> ).left_join(
> :manufacturers,
> Sequel[:cars][:manufacturer_id] => Sequel[:manufacturers][:id]
> )
> end end I am able to load association with additional attribute via 
> car.company_dataset.first,
> but surprisingly car.company returns nil.
>

Can you post what SQL is being generated? Might be something like SELECT *,
cars.manufacturer AS producer, which is not going to be correct if you are
joining.  You might need to use something like:
select_all(:companies).select_append(...)


> What worked for me is to add optional after_load where after association
> is loaded I set a value with separate method.
> However, to be honest, I don't like this approach with separate method
> definition to handle this case. I am wondering if there is any smarter
> solution for this?
>

Hard to say if there is a better solution, since you didn't provide a self
contained example and I'm not sure exactly what it is causing your
problem.  If it's just an SQL issue as I suspect, then fixing the SQL may
solve the problem nicely.

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/CADGZSSd3cg%3DQv4RSqmeVSvzAezqm9-y5CdhTLMk-1C95NytPsg%40mail.gmail.com.

Reply via email to