Shane Sherman
2005-Oct-27 21:48 UTC
has_one with Single Table Inheritance - NameError : uninitialized constant
I am having a problem with using SIngle Table Inheritance with a has_one
relationship, and I am at a loss of what could be causing it. Below is the
related code and the error message.
class User < ActiveRecord::Base
has_one :wishlist
end
class List < ActiveRecord::Base
belongs_to :user
end
class Wishlist < List
has_and_belongs_to_many :items, :class_name => ''Album'',
:join_table =>
''items_lists'', :foreign_key => ''list_id'',
:association_foreign_key =>
''item_id''
end
Whenever I try to access user.wishlist I get the follow exception :
g:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.1/lib/active_support/dependencies.rb:198:in
`const_missing''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations/association_proxy.rb:11:in
`initialize''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations/belongs_to_association.rb:6:in
`eval''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations/association_proxy.rb:11:in
`initialize''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations/belongs_to_association.rb:6:in
`initialize''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations/has_one_association.rb:5:in
`initialize''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations.rb:637:in
`new''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations.rb:637:in
`wishlist''
g:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.12.1/lib/active_record/associations.rb:633:in
`wishlist''
./script/../config/../app/models/user.rb:57
line 57 from user.rb is :
def add_to_wishlist(album)
* if self.wishlist.nil?*
wishlist = Wishlist.new(''title''=>''Default Wish
List'')
wishlist.user = self
self.wishlist = wishlist
end
self.wishlist.items<<album
self.wishlist.save
end
What''s strange is this works perfectly in my Wishlist unit test.
def test_add_to_wishlist
assert_nil @normal_user.wishlist
album = Album.find(1)
@normal_user.add_to_wishlist(album)
assert_not_nil @normal_user.wishlist
assert_equal 1, @normal_user.wishlist.items.length
assert_equal album, @normal_user.wishlist.items[0]
end
no errors. passes all assertions. any ideas?
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails
Reasonably Related Threads
- Hitting unknown error with "can't dup NilClass"
- Problems with association named :task
- attachment_fu and/or rmagick on osx -- weird error
- Paperclip attachment to AWS - ActiveRecord::AssociationTypeMismatch Going Nuts!
- Paperclip attachment to AWS - ActiveRecord::AssociationTypeMismatch
