Files
openfoodnetwork/app/models/schedule.rb
Pau Perez 1903134e13 Add timestamps to OC schedule join table
This is critical to debug bugs related to subscriptions.

Essentially, `has_and_belongs_to_many` doesn't give us the option for
any other column that the foreign keys themselves:

> A has_and_belongs_to_many association creates a direct many-to-many
> connection with another model, with no intervening model.

Source: https://guides.rubyonrails.org/v3.2/association_basics.html#the-has_and_belongs_to_many-association

Note however, that there's no way to update an order_cycle_schedule,
that I can think of but `updated_at` doesn't do any harm.
2020-04-30 15:00:13 +02:00

18 lines
678 B
Ruby

class Schedule < ActiveRecord::Base
has_paper_trail meta: { custom_data: proc { |schedule| schedule.order_cycle_ids.to_s } }
has_many :order_cycles, through: :order_cycle_schedules
has_many :order_cycle_schedules, dependent: :destroy
has_many :coordinators, uniq: true, through: :order_cycles
attr_accessible :name, :order_cycle_ids
validates :order_cycles, presence: true
scope :with_coordinator, lambda { |enterprise| joins(:order_cycles).where('coordinator_id = ?', enterprise.id).select('DISTINCT schedules.*') }
def current_or_next_order_cycle
order_cycles.where('orders_close_at > (?)', Time.zone.now).order('orders_close_at ASC').first
end
end