ruby on rails - validates_length_of with a variable -



ruby on rails - validates_length_of with a variable -

i have activerecord model:

class message < activerecord::base

which has 2 attributes: content(string) , max_content_length(integer) both dynamic , set user. i'm trying validate length of content string using max_content_length integer so:

validates_length_of :content, maximum: self.max_content_length

or so:

validates_length_of :content, maximum: lambda{ self.max_content_length }

but validates_length_of throws either "no method found" or "maximum must nonnegative integer or infinity" exception (respectively).

is there other way "register" max_content_length function validator length?

thank you!

(i know question perchance duplicate this question , this one unfortunately answers there little off-topic , confusing)

make own validation:

class message < activerecord::base validate :validate_content_length private def validate_content_length if content.length > max_content_length errors.add :content, "too long (must #{max_content_length} characters or less)" end end end

help guides here http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations

ruby-on-rails ruby activerecord

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -