Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2012 12:17
Show Gist options
  • Save anonymous/3911436 to your computer and use it in GitHub Desktop.
Save anonymous/3911436 to your computer and use it in GitHub Desktop.
class Organization < ActiveRecord::Base
acts_as_nested_set
attr_accessible :name, :location_id, :parent_id
belongs_to :location
has_many :batches
has_many :org_prod_relationships, dependent: :destroy
has_many :products, through: :org_prod_relationships
validates :name, presence: true
validates :location_id, presence: true
def has_product?(prod)
org_prod_relationships.find_by_product_id(prod.id)
end
def add_product!(prod)
org_prod_relationships.create!(product_id: prod.id)
end
def publish_product!(prod)
self.descendants.each do |d|
d.add_product!(prod)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment