Polymorphic Validation Problem

Hello all,

I am implementing tagging on my site. The problem I am experiencing is
that when I apply tags to a Blog model object, if one of the tags is
invalid, it doesn’t raise any type of error in the blog model…it just
silently fails. Here’s what my models look like.

class Blog < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings
end

class Tag < ActiveRecord::Base
has_many :taggings
validates_format_of :name, :with => /^[a-zA-Z0-9]*$/, :message =>
“Invalid characters in tag.”

def on(taggable)
tagging = taggings.create :taggable => taggable
end
end

class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :blog
belongs_to :taggable, :polymorphic => true

def self.tagged_class(taggable)
ActiveRecord::Base.send(:class_name_of_active_record_descendant,
taggable.class).to_s
end

def self.find_taggable(tagged_class, tagged_id)
tagged_class.constantize.find(tagged_id)
end
end

Here’s what my controller test code looks like to simulate the
condition.
def test
blog = Blog.find(:first)
blog.tag_with(“in–@idtag”)
raise blog.valid?.inspect # => true, but I want it to be false
end

This is driving me batty and it’s probably just something simple, but 2
hours of research didn’t pull anything up. Thanks ahead of time if
anyone can point me in the right direction.

Hmm - I think:
Change “blog.valid?.inspect” to “blog.valid?”

From what I can see inspect turn the ouput into a printable string.
The string “true” is not the same as the boolean TRUE:

news = News.find(5)
news.valid?
=> true
news.valid?.inspect
=> “true”
news.valid?.inspect == TRUE
=>false