I’m using composed_of in one of my models, and I’d like to validate
that the attribute is present.
class Hand < AR::Base
composed_of :level, :mapping => [ %w(sb sb), %w(bb bb), %w(ante ante)
validates_presence_of :level, :message => “must be set”
The Level class itself is very simple:
attr_reader :sb, :bb, :ante
def initialize(sb, bb, ante)
@sb, @bb, @ante = sb.to_i, bb.to_i, ante.to_i
raise “SB can’t be 0” unless @sb > 0
raise “BB can’t be 0” unless @bb > 0
raise “SB can’t be bigger than BB” if @sb > @bb
raise “Ante can’t be bigger than SB” if @ante > @sb
The problem is that if create a new hand and don’t set the level
object, calling valid? results in the level object being created with
null values. null gets turned into 0 which raises an error.
The behavior I want is to simply have valid? return false, saying that
the reason is that level is null. Can I do that?