vendredi 15 mai 2015

How can I dynamically define a source for an ActiveRecord has_many relation?

Given the ActiveRecord model below:

class Model < ActiveRecord::Base
  has_many :group_assignments
  has_many :product_assignments
  # Only one of the next two should appear, based on condition
  has_many :products, through: :product_assignments
  has_many :products, through: :group_assignments
end

Would like to define the last has_many...through relation based on the following condition: if the model has any product assignments associated to it, then products come from product assignments. If the model has no product_assignments, then products come from group_assignments.

Aucun commentaire:

Enregistrer un commentaire