Is it possible to share constants between controller and model?
e.g. in product.rb
I have
PRODUCT_TYPES = %w[one two]
I want PRODUCT_TYPES
constant to be available in the controllers as well.
Is it possible to share constants between controller and model?
e.g. in product.rb
I have
PRODUCT_TYPES = %w[one two]
I want PRODUCT_TYPES
constant to be available in the controllers as well.
As per my consideration your Product model looks like
class Product < ActiveRecord::Base
PRODUCT_TYPES = %w[one two]
end
You can access the given constant in the controller as below
p.product_type == Product::PRODUCT_TYPES[:one]
I deeply explained the various possibility in this answer
You basically have 3 possibilities:
Class::CONSTANT
)We can access model constant in Controller as bellow...
class Invoice < ActiveRecord::Base
STATUS_PAYABLE = %w(APPROVED OPEN)
end
Controller use like ..
Invoice::STATUS_PAYABLE.include?('OPEN')