views:

30

answers:

1

A User can add a Sentence directly on my website, via Twitter or email. To add a sentence they must have a minimum score. If they don't have the minimum score they can't post the sentence and a warning message is either flashed on the website, sent back to them via Twitter or email.

So I'm wondering how best to code this check. Im thinking a sentence observer. So far my thoughts are in before_create

score_sufficient()

  • score ok => save
  • score too low => do not save

In the case of too low I need to return some flag so that the calling code can then fire off teh relevant warning.

What type of flag should I return? False is too ambiguous as that could refer to validation. I could raise an exception but that doesn't sound right or I could return a symbol? Is this even the right approach?

What's the best way to code this?

+1  A: 

No need for an observer, just use a before_create filter.

class Sentence < ActiveRecord::Base
  before_create :check_score
  def check_score
    errors.add_to_base("Score too low") unless score >= 50
  end
end
Samuel