Question

I am using Rails 3.0 and have several models which have a public, English, reference field such as a title on a chapter of a story. I am looking for an elegant way to implement certain stop words for actions on controllers, e.g. "new", "update", "destroy", etc. What is the best way to do this, via some kind of callback during the saving of the model?

Was it helpful?

Solution

Rails has a tonne of callbacks for exactly this kind of thing. I think the callbacks that would be of most interest to you would be before_validation, before_validation_on_create, after_validation and after_validation_on_create since you can get in a strip out stopwords and then have your validations run on whats left.

So your Chapter model would have something like:

before_validation_on_create :check_for_stop_words
def check_for_stop_words
  #checks for stopwords...
end

The usual before_destroy and before_update are also there if you need them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top