Thanks for your help Jeremy. I tried the delayed evaluation route, but found it make the code more complex than it was worth. In the end, I've gone with making a model based on the "OrderLine" equivalent in the actual code, with a "default scope" method in the dataset_module that accepts the product_name equivalent as an argument. It means all the joined columns aren't actually "official" parts of the model, but by making methods that wrap the values hash to expose the joined fields as if they really were "official", I'm able to create relationships to other models successfully with the wrapped columns.
Glad I saw the extend_datasets example though - that's going to come in handy at some stage! Appreciate your help, Beth On Sun, Dec 22, 2019 at 12:20 PM Jeremy Evans <[email protected]> wrote: > On Friday, December 20, 2019 at 11:33:21 PM UTC-8, Beth Skurrie wrote: >> >> Thanks Jeremy. >> >> The delay almost works. >> >> DenormalisedOrderLine3.dataset.clone(product_name: "Fish") >> => #<Sequel::SQLite::Dataset: "SELECT * FROM (SELECT `invoice_number`, >> `product_name`, `customer_id` FROM `orders` LEFT OUTER JOIN (SELECT * FROM >> `order_lines` WHERE (`product_name` IS NULL)) AS 't1' ON (`t1`.`order_id` = >> `orders`.`id`)) AS 'orders'"> >> >> Unfortunately, the opts aren't picked up in the joined dataset. It works >> if I delay/clone the joined dataset directly, but then I'm back to not >> being able to create a class out of it. >> >> DELAYED_ORDER_LINES = DATABASE[:order_lines].where(product_name: >> Sequel.delay{|ds| ds.opts[:product_name]}) >> DELAYED_ORDER_LINES.clone(product_name: "Fish") >> => #<Sequel::SQLite::Dataset: "SELECT * FROM `order_lines` WHERE >> (`product_name` = 'Fish')"> >> > > Ah, this is because the subqueries are separate datasets. I think this > will fix things, by passing the product_name option to internal datasets > during the SQL generation process: > > DB.extend_datasets do > def subselect_sql_dataset(sql, ds) > ds = super > if product_name = @opts[:product_name] > ds = ds.clone(product_name: product_name) > end > ds > end > end > > Thanks, > Jeremy > > -- > You received this message because you are subscribed to a topic in the > Google Groups "sequel-talk" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/sequel-talk/QiQdNkw3Tv0/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sequel-talk/c55e1867-3974-4707-92b7-cf1ca3584dd8%40googlegroups.com > <https://groups.google.com/d/msgid/sequel-talk/c55e1867-3974-4707-92b7-cf1ca3584dd8%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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 view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/CACO5b1YFz-yDrV0%3Db%3DP8onkqxgaXcfLuVpRD%3DoQ8cBbxXT5Z%2BQ%40mail.gmail.com.
