I was trying to simplify the problem with my typicasl car/wheel analogy. I really need it this way, because:
* I want to benefit from association memoization * There is this complicated index where diverse stuff is being loaded, and I wanted to take advantage of the .eager method to eagerload these type of things at once. My workaround was a variation of the 1 => 0 condition, but null_dataset might be better for what I need, I'll definitely have a look. thx! 2016-06-16 16:26 GMT+02:00 Jeremy Evans <[email protected]>: > > > On Thursday, June 16, 2016 at 7:12:22 AM UTC-7, Tiago Cardoso wrote: >> >> I'm trying to create these "custom" associations based on another one, a >> collection. Basically they are: >> >> * the collection >> * the first element after applying some conditions >> * the last element after the first element >> >> class Car < Sequel::Model >> one_to_many :wheels >> one_to_one :first_wheel, class: :Wheel do |ds| >> ds.pumped >> end # this one returns me the first pumped wheel >> one_to_one :last_wheel, class: :Wheel do |ds| >> if first_wheel >> date = first_wheel.created_at >> ds.where { created_at > date }.inverse # return me the last wheel >> installed >> else >> # this is the issue. if i say "nil", it'll complain because it >> doesn't respond to .all >> end >> end >> >> So basically, I want last_wheel to be nil when there is no first_wheel. >> So: >> >> * either I put the first_wheel query as a subquery for the second one >> (would be great, but I don't know how to retrieve it from the association). >> * or I somehow return something that "quacks" like an empty dataset >> * some 3rd option that you might suggest >> >> I just wanted to also say that I'm not in favour of building these >> associations as methods inside the collection association, as I'll need >> eagerloading. >> >> Is something like this already supported? >> > > How many wheels can a car have? Unless it's some large number, just have > a wheels association, and add a first_wheel and last_wheel method that uses > the wheels association. > > If you really much do it the way you are doing it, you can either use a > condition that's always false (e.g. {1=>0}) or use the null_dataset > extension. > > 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/lz2i8P0Vleg/unsubscribe. > To unsubscribe from this group and all its topics, 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. > -- 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.
