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 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.

Reply via email to