mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
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.
18 lines
678 B
Ruby
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
|