How can I make Rails ActiveRecord automatically truncate values set to attributes with maximum length? -
assuming have class such following:
class book < activerecord::base validates :title, :length => {:maximum => 10} end
is there way (gem install?) can have activerecord automatically truncate values according maximum length?
for instance, when write:
b = book.new b.title = "123456789012345" # longer maximum length of title 10 b.save
should save , return true?
if there not such way, how suggest proceed facing such problem more generally?
well, if want value truncated if long, don't need validation, because pass. i'd handle this:
class book < activerecord::base before_save :truncate_values def truncate_values self.title = self.title[0..9] if self.title.length > 10 end end
Comments
Post a Comment