On Saturday, April 29, 2017 at 1:05:02 PM UTC-7, dota? =op wrote:
>
> > I'm not sure exactly what you want. My guess is you either want to pass
> a
> > block to one_to_many, or to use the :dataset option Can you please
> provide
> > a minimal self contained example, as well as the SQL you want to
> generate?
>
> here is script:
>
> require 'sequel'
>
> Sequel.extension :s
> Sequel::Model.plugin :association_proxies
> Sequel::Model.plugin :dataset_associations
> Object.send(:include, Sequel::S)
>
> DB = Sequel.sqlite
>
> DB.create_table :zoings do
> primary_key :id
> foreign_key :blorl_id, :blorls
> DateTime :t
> end
>
> DB.create_table :blorls do
> primary_key :id
> DateTime :magic_t
> end
>
> class Zoing < Sequel::Model
> end
>
> class Blorl < Sequel::Model
> one_to_many :zoings
>
> one_to_many(
> :magic_zoings,
> clone: :zoings,
> graph_block: proc { |j, lj, js| S(j)[:t] < S(lj)[:magic_t] }
> )
>
> one_to_many(
> :condition_magic_zoings,
> clone: :magic_zoings,
> conditions: proc { created_at < magic_t } # wat
> )
>
> one_to_many(
> :block_magic_zoings,
> clone: :magic_zoings
> ) do |ds|
> ds.where(S(:t) < magic_t) # crash for dataset_associations
> end
>
> one_to_many(
> :dataset_magic_zoings,
> clone: :magic_zoings,
> dataset: proc do |r|
> # doesnt work for dataset_associations
> Blorl
> .association_join(:dataset_magic_zoings)
> .with_row_proc(Zoing)
> .select_all(:dataset_magic_zoings)
> end
> )
>
> one_to_many(
> :works_magic_zoings,
> clone: :magic_zoings
> ) do |ds|
> # works, but always join
> Blorl
> .association_join(:works_magic_zoings)
> .with_row_proc(Zoing)
> .select_all(:works_magic_zoings)
> end
> end
>
> t = Time.now
> blorl = Blorl.create(magic_t: t)
> (-10..+10).each {|u| blorl.add_zoing(t: t+u)}
>
> def ev4l(x, y)
> %W(
> Blorl.first.#{x}.#{y}
> Blorl.association_join(:#{x}).#{y}
> Blorl.#{x}.#{y}
> ).each do |l|
> s =
> begin
> eval(l)
> rescue => e
> e.class.name.to_s
> end
>
> puts("#{l} # => #{s}")
> end
> end
>
> Blorl.first.block_magic_zoings.count
>
> puts("#")
> puts("# only joins")
> puts("#")
> ev4l('magic_zoings', 'sql')
> puts("")
> ev4l('magic_zoings', 'count')
> puts("")
>
> puts("#")
> puts("# just broken")
> puts("#")
> ev4l('condition_magic_zoings', 'sql')
> puts("")
> ev4l('condition_magic_zoings', 'count')
> puts("")
>
> puts("#")
> puts("# crash dataset_associations")
> puts("#")
> ev4l('block_magic_zoings', 'sql')
> puts("")
> ev4l('block_magic_zoings', 'count')
> puts("")
>
> puts("#")
> puts("# doesnt work for dataset_associations")
> puts("#")
> ev4l('dataset_magic_zoings', 'sql')
> puts("")
> ev4l('dataset_magic_zoings', 'count')
> puts("")
>
> puts("#")
> puts("# works but always join")
> puts("#")
> ev4l('works_magic_zoings', 'sql')
> puts("")
> ev4l('works_magic_zoings', 'count')
> puts("")
>
>
> here is output:
>
> #
> # only joins
> #
> Blorl.first.magic_zoings.sql # => SELECT * FROM `zoings` WHERE
> (`zoings`.`blorl_id` = 1)
> Blorl.association_join(:magic_zoings).sql # => SELECT * FROM
> `blorls` INNER JOIN `zoings` AS 'magic_zoings' ON
> ((`magic_zoings`.`blorl_id` = `blorls`.`id`) AND (`magic_zoings`.`t` <
> `blorls`.`magic_t`))
> Blorl.magic_zoings.sql # => SELECT * FROM `zoings` WHERE
> (`zoings`.`blorl_id` IN (SELECT `blorls`.`id` FROM `blorls`))
>
> Blorl.first.magic_zoings.count # => 21
> Blorl.association_join(:magic_zoings).count # => 10
> Blorl.magic_zoings.count # => 21
>
> #
> # just broken
> #
> Blorl.first.condition_magic_zoings.sql # => SELECT * FROM `zoings`
> WHERE ((`created_at` < `magic_t`) AND (`zoings`.`blorl_id` = 1))
> Blorl.association_join(:condition_magic_zoings).sql # => SELECT *
> FROM `blorls` INNER JOIN `zoings` AS 'condition_magic_zoings' ON
> ((`condition_magic_zoings`.`blorl_id` = `blorls`.`id`) AND
> (`condition_magic_zoings`.`t` < `blorls`.`magic_t`))
> Blorl.condition_magic_zoings.sql # => SELECT * FROM `zoings` WHERE
> ((`zoings`.`blorl_id` IN (SELECT `blorls`.`id` FROM `blorls`)) AND
> (`created_at` < `magic_t`))
>
> Blorl.first.condition_magic_zoings.count # => Sequel::DatabaseError
> Blorl.association_join(:condition_magic_zoings).count # => 10
> Blorl.condition_magic_zoings.count # => Sequel::DatabaseError
>
> #
> # crash dataset_associations
> #
> Blorl.first.block_magic_zoings.sql # => SELECT * FROM `zoings`
> WHERE ((`zoings`.`blorl_id` = 1) AND (`t` < '2017-04-29
> 16:46:49.750731'))
> Blorl.association_join(:block_magic_zoings).sql # => SELECT * FROM
> `blorls` INNER JOIN `zoings` AS 'block_magic_zoings' ON
> ((`block_magic_zoings`.`blorl_id` = `blorls`.`id`) AND
> (`block_magic_zoings`.`t` < `blorls`.`magic_t`))
> Blorl.block_magic_zoings.sql # => NameError
>
> Blorl.first.block_magic_zoings.count # => 10
> Blorl.association_join(:block_magic_zoings).count # => 10
> Blorl.block_magic_zoings.count # => NameError
>
> #
> # doesnt work for dataset_associations
> #
> Blorl.first.dataset_magic_zoings.sql # => SELECT
> `dataset_magic_zoings`.* FROM `blorls` INNER JOIN `zoings` AS
> 'dataset_magic_zoings' ON ((`dataset_magic_zoings`.`blorl_id` =
> `blorls`.`id`) AND (`dataset_magic_zoings`.`t` < `blorls`.`magic_t`))
> Blorl.association_join(:dataset_magic_zoings).sql # => SELECT *
> FROM `blorls` INNER JOIN `zoings` AS 'dataset_magic_zoings' ON
> ((`dataset_magic_zoings`.`blorl_id` = `blorls`.`id`) AND
> (`dataset_magic_zoings`.`t` < `blorls`.`magic_t`))
> Blorl.dataset_magic_zoings.sql # => SELECT * FROM `zoings` WHERE
> (`zoings`.`blorl_id` IN (SELECT `blorls`.`id` FROM `blorls`))
>
> Blorl.first.dataset_magic_zoings.count # => 10
> Blorl.association_join(:dataset_magic_zoings).count # => 10
> Blorl.dataset_magic_zoings.count # => 21
>
> #
> # works but always join
> #
> Blorl.first.works_magic_zoings.sql # => SELECT
> `works_magic_zoings`.* FROM `blorls` INNER JOIN `zoings` AS
> 'works_magic_zoings' ON ((`works_magic_zoings`.`blorl_id` =
> `blorls`.`id`) AND (`works_magic_zoings`.`t` < `blorls`.`magic_t`))
> Blorl.association_join(:works_magic_zoings).sql # => SELECT * FROM
> `blorls` INNER JOIN `zoings` AS 'works_magic_zoings' ON
> ((`works_magic_zoings`.`blorl_id` = `blorls`.`id`) AND
> (`works_magic_zoings`.`t` < `blorls`.`magic_t`))
> Blorl.works_magic_zoings.sql # => SELECT `works_magic_zoings`.*
> FROM `blorls` INNER JOIN `zoings` AS 'works_magic_zoings' ON
> ((`works_magic_zoings`.`blorl_id` = `blorls`.`id`) AND
> (`works_magic_zoings`.`t` < `blorls`.`magic_t`))
>
> Blorl.first.works_magic_zoings.count # => 10
> Blorl.association_join(:works_magic_zoings).count # => 10
> Blorl.works_magic_zoings.count # => 10
>
>
> and here is wat i want:
>
> something that works liek teh last case, but use WHERE instead of
> JOIN on Blorl.first.works_magic_zoings.
>
I would use block_magic_zoings, and just define a custom dataset method:
Blorl.dataset_module do
def block_magic_zoings
Zoing.join(:blorls, :id=>:blorl_id){ |j, lj, js| S(lj)[:t] <
S(j)[:magic_t] }.where(:blorl_id=>select(:id))
end
end
dataset associations that are instance specific is not something that
Sequel can handle by default. We could punt and support an association
option that would allow you to override how dataset_associations handles
things, but I think it's just better to define your own dataset method in
that case, as I did here.
Note that dataset_associations_join only affects associations that use join
tables, and one_to_many does not use a join 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 [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.