I would keep the one_to_many :subscriptions association and then define the
one_to_one as you did, but define an `active_subscription` one_to_one.
one_to_one :active_subscription, class: 'DonorSee::Subscription',
conditions: { active: true }
Untested but I'm assuming it will work.
On Wednesday, April 19, 2017 at 1:46:02 PM UTC-4, Barbara Carradini wrote:
>
> I have an alternate solution that I believe is elegant enough. I am going
> to define the standard one_to_many association between User and
> Subscription:
>
> class User < Sequel::Model
> one_to_many :subscriptions
> end
>
> Then simply define a method for the User model that gets me what I want:
>
> def subscription
> subscriptions_dataset.first(active: true)
> end
>
> Thanks anyway!
>
>
> On Wednesday, April 19, 2017 at 1:33:04 PM UTC-4, Barbara Carradini wrote:
>>
>> I have a User model and a Subscription model. The Subscription model
>> contains, among other things, a foreign key that points back to User
>> (user_id) and a Boolean (active) that specifies whether the Subscription is
>> active or inactive. There can only be one active Subscription for a User at
>> any time, though there may be multiple canceled/inactive Subscriptions for
>> that user. I'd like to specify an association for the User model that grabs
>> the first active, associated Subscription (which, because of other measures
>> I've taken will be the ONLY active, associated Subscription). I can almost
>> get there with the following in the User model:
>>
>> one_to_one :subscription, :class=>'DonorSee::Subscription',
>> :order=>:active, :limit=>1
>>
>> However, this returns the first *inactive* Subscription, since false
>> orders before true. I want the first *active* Subscription. I've tried
>> the following:
>>
>> one_to_one :subscription, :class=>'DonorSee::Subscription',
>> :reverse_order=>:active, :limit=>1
>>
>> but I still get the first inactive Subscription when I invoke
>> user.subscription:
>>
>>
>> Here are my models (simplified):
>>
>> class Subscription < Sequel::Model
>> many_to_one :user
>> end
>>
>> class User < Sequel::Model
>> one_to_one :subscription, class: 'DonorSee::Subscription',
>> :reverse_order=>:active, :limit=>1
>> end
>>
>>
--
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.