I think you should remove the database restriction and handle this by using a setter that truncates to the wanted length by default. In you model add excerpt_setter
to the attr_accessible
list. And then define it like this
def excerpt_setter=(str)
self.excerpt = truncate(str, :length => 114)
end
def excerpt_setter
self.excerpt
end
And then in the controller
def create
@blogpost = Blogpost.new(blogpost_params)
@blogpost.excerpt_setter = truncate(@excerpt.body, :length => 114)
respond_to do |format|
if @blogpost.save
etc,etc,
end
Another thing: You can also define a excerpt
method in your model and drop the field if there isnt any good reason to store a part of the body in another field.
include ActionView::Helpers::TextHelper # this is needed to make the truncate method avaiable in model
...
...
...
def excerpt
truncate(self.body, :length => 114)
end
If you dont need the data stored in the database for performence reasons this whould be my prefered solution.