Prev | Current Page 326 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


In the following example, we raise an AssociationTypeMismatch error when we attempt to
add a dog object to our pet collection, because our pet collection only has_many cats; that is,
there is no association to the Dog class:
# program to raise an AssociationTypeMismatch error
require 'rubygems'
require 'activerecord'
ActiveRecord::Base.establish_connection(:adapter => "sqlserver",
:host => "mydbhost.com", :database => "test", :username => "sa", :password => "")
class Pet < ActiveRecord::Base
has_many :cats
end
CHAPTER 6 ?–  ACTIVE RECORD TESTING AND DEBUGGING 146
class Cat < ActiveRecord::Base
belongs_to :pet
end
class Dog
attr_accessor :name
end
temp = Pet.find(1)
p1 = Dog.new
p1.name = "Abby"
temp << p1 # => raises the AssociationTypeMismatch error
SerializationTypeMismatch
This error is raised when you attempt to assign an object of the wrong type into a field that is
to be serialized within the database.
The following example generates a SerializationTypeMismatch error, because we state
that data in our yamldata field should be of Array type, but within our actual program, we
attempt to assign a Hash instead.


Pages:
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338