Validate that an object has one or more associated objects

There is a validation that will check the length of your association. Try this:

class Product < ActiveRecord::Base
  has_many :product_categories
  has_many :categories, :through => :product_categories

  validates :categories, :length => { :minimum => 1 }
end

Leave a Comment

Validate that an object has one or more associated objects

There is a validation that will check the length of your association. Try this:

class Product < ActiveRecord::Base
  has_many :product_categories
  has_many :categories, :through => :product_categories

  validates :categories, :length => { :minimum => 1 }
end

Leave a Comment