I need to ensure that when a product is created it has atleast one category. I could do this with a custom validation class, but I was hoping there was a more standard way of doing it.
class Product < ActiveRecord::Base
has_many :product_categories
has_many :categories, :through => :product_categories #must have at least 1
end
class Category < ActiveRecord::Base
has_many :product_categories
has_many :products, :through => :product_categories
end
class ProductCategory < ActiveRecord::Base
belongs_to :product
belongs_to :category
end
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
Ensures it has at least one category:
class Product < ActiveRecord::Base
has_many :product_categories
has_many :categories, :through => :product_categories
validates :categories, :presence => true
end
I find the error message using :presence
is clearer than using length minimum 1
validation
Instead of wpgreenway's solution, I would suggest to use a hook method as before_save and use a has_and_belongs_to_many association.
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
before_save :ensure_that_a_product_belongs_to_one_category
def ensure_that_a_product_belongs_to_one_category
if self.category_ids < 1
errors.add(:base, "A product must belongs to one category at least")
return false
else
return true
end
end
class ProductsController < ApplicationController
def create
params[:category] ||= []
@product.category_ids = params[:category]
.....
end
end
And in your view, use can use for example options_from_collection_for_select
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With