mrkylehayes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Feb-25 19:26 UTC
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="elvis-u/OeZctXj4z2eFz/2MeuCQ@public.gmane.org"
elvis.email="elvis-u/OeZctXj4z2eFz/2MeuCQ@public.gmane.org"
=>
"elvis-u/OeZctXj4z2eFz/2MeuCQ@public.gmane.org">> address=Address.new("3734 Elvis Presley Blvd",
"Memphis", "Tennessee", 38118)
address=Address.new("3734 Elvis Presley Blvd", "Memphis",
"Tennessee",
38118)
=> #<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:in `sort''
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:in `clone_attributes''
...
Why is the method ''<=>'' being attempted on the zip
field?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-26 07:41 UTC
Re: NoMethodError: undefined method `<=>'' for :zip:Symbol
On 25 Feb 2008, at 19:26, mrkylehayes-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > 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 > > > 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:in `sort'' > 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: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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---