Just to be clear the final syntax should be like this:

WorkOrder.one_to_many :jobs, key: [:worker_id, :schedule_id], primary_key: 
[:worker_id, :schedule_id]
Job.many_to_one :work_order, key: [:worker_id, :schedule_id], primary_key: 
[:worker_id, :schedule_id]

A follow up question, if I've got another association that works like this:

Order.many_to_one :property
Order.many_to_one :service
Order.one_through_one :city, :join_table=>:properties, :left_key=>:id, 
:left_primary_key=>:property_id, :right_key=>:city_id

Property.many_to_one :city
City.one_to_many :offers


Offer.many_to_one :city
Offer.many_to_one :service

I can load the Offer for the Order similar to a compound key: 
Offer.where(city: order.city, service: order.service)

But in this case there's no city_id on the order itself, the city_id 
belongs to the joined community. Is it possible to connect Order <-> Offer 
as a sequel model association?

Thanks for all your help, Jeremy!

Andrew

On Sunday, June 21, 2015 at 4:48:12 PM UTC-4, Jeremy Evans wrote:
>
> On Sunday, June 21, 2015 at 8:04:16 AM UTC-7, Andrew Burleson wrote:
>>
>> Given the following models:
>>
>> Job.many_to_one :worker
>> Job.many_to_one :schedule
>>
>> Worker.one_to_many :work_order
>>
>> WorkOrder.many_to_one :worker
>> WorkOrder.many_to_one :schedule
>>
>> A Schedule represents a timeframe, for example one week, and a WorkOrder 
>> represents all the jobs for a specific worker for that specific schedule.
>>
>> It's easy enough to write a query for all the jobs that belong to a 
>> specific work order:
>>
>> Job.where(schedule: work_order.schedule, worker: work_order.worker)
>>
>> ...and from the docs (
>> http://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html) 
>> it looks like you should be able to do this:
>>
>> WorkOrder.one_to_many :jobs, key: [:worker_id, :schedule_id]
>> Job.many_to_one :work_order, key: [:worker_id, :schedule_id]
>>
>> But when trying this I get:
>>
>> mismatched number of keys: [:worker_id, :schedule_id] vs [:id] 
>> (Sequel::Error)
>>
>> So, can you write an association like this, and if so how so?
>>
>
> You need to specify the :primary_key=>[:worker_id, :schedule_id] option as 
> well, since that isn't the primary key of the associated class.
>
> 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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to