NoMethodError: undefined method `<=>' for :zip:Symbol

Long time programmer, but I just started learning RoR last week. I am
going through some examples in the book “Ruby on Rails: Up and
Running”.

The example I am on had me add a table to the database that looks like
so:
CREATE TABLE people (
id int(11) NOT NULL auto_increment,
type varchar(20) default NULL,
name varchar(20) default NULL,
email varchar(30) default NULL,
street_address varchar(30) default NULL,
city varchar(30) default NULL,
state varchar(20) default NULL,
zip int(5) default NULL,
camera varchar(20) default NULL,
PRIMARY KEY (id)
)

In addition, I created the following two classes:

class Person < ActiveRecord::Base
composed_of :address, :class_name => “Address”,
:mapping => [[:street_address, :street_address],
[:city, :city],
[:state, :state],
[:zip, :zip]]
end

class Address
def initialize(street_address, city, state, zip)
@street_address = street_address
@city = city
@state = state
@zip = zip
end

attr_reader :street_address, :city, :state, :zip
end

Finally, it had me run the following commands in Console:

elvis.name=Person.new
elvis.name=Person.new
NameError: undefined local variable or method `elvis’ for #<Object:
0x284f9e8>
from (irb):1

elvis=Person.new
elvis=Person.new
=> #<Person id: nil, type: nil, name: nil, email: nil, street_address:
nil, city: nil, state: nil, zip: nil, camera: nil>

elvis.name=“Elvis Presley”
elvis.name=“Elvis Presley”
=> “Elvis Presley”

elvis.email=“[email protected]
elvis.email=“[email protected]
=> “[email protected]

address=Address.new(“3734 Elvis Presley Blvd”, “Memphis”, “Tennessee”, 38118)
address=Address.new(“3734 Elvis Presley Blvd”, “Memphis”, “Tennessee”,

=> #<Address:0x3f3ce94 @street_address=“3734 Elvis Presley Blvd”,
@zip=38118, @state=“Tennessee”, @city=“Memphis”>

elvis.address=address
elvis.address=address
=> #<Address:0x3f3ce94 @street_address=“3734 Elvis Presley Blvd”,
@zip=38118, @state=“Tennessee”, @city=“Memphis”>

elvis.save
elvis.save

However, when I ran the latter-most command, “elvis.save” I received
the following (abbreviated) error:

NoMethodError: undefined method <=>' for :zip:Symbol from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ active_record/base.rb:2177:insort’
from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2177:in attribute_names' from C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/ active_record/base.rb:2458:inclone_attributes’

Why is the method ‘<=>’ being attempted on the zip field?

On 25 Feb 2008, at 19:26, [email protected] wrote:

active_record/base.rb:2458:in `clone_attributes’

Why is the method ‘<=>’ being attempted on the zip field?

it’s not calling <=> on the zip field itself but on the mapping you’ve
supplied, which is expected to contain strings

:mapping => [%w(street_address street_address),
%w(city city),
%w(state state),
%w(zip zip)]
Should do the trick

Fred