Jacob Helwig
2009-Oct-17 21:49 UTC
undefined local variable or method `autosave_associated_records_for_book'' when saving record
So, I''m at a loss trying to figure out where I screwed something up here. I''ve got (what I think should be) a fairly straight-forward record setup, but when I try to create/save a BookEdition record, I get the following error: NameError: undefined local variable or method `autosave_associated_records_for_book_editions_users'' I''m actually getting two slightly different errors, but with almost the exact same output: ---------------------------------------------------------------------- 1) Error: test_create_valid_book_edition(BookEditionTest): NameError: undefined local variable or method `autosave_associated_records_for_book_editions_users'' for #<ActiveSupport::Callbacks::Callback:0xb6e86b40> /test/unit/book_edition_test.rb:11:in `test_create_valid_book_edition'' 2) Error: test_relates_to_book_editions_and_amazon_images(BookEditionsImageTest): NameError: undefined local variable or method `validate_associated_records_for_book'' for #<ActiveSupport::Callbacks::Callback:0xb6e88b70> vendor/gems/notahat-machinist-1.0.3/lib/machinist/active_record.rb:54:in `make'' /test/unit/book_editions_image_test.rb:5:in `test_relates_to_book_editions_and_amazon_images'' ---------------------------------------------------------------------- The full project (with the failures) is up on GitHub ( http://github.com/jhelwig/kamitsukai/tree/wip ), in case I miss something that would help in tracking this down. Here are the two unit tests where I''m seeing this kind of error crop up: test/units/book_edition_test.rb ---------------------------------------------------------------------- require ''test_helper'' class BookEditionTest < ActiveSupport::TestCase test "create valid book edition" do b = Book.make e = BookEdition.new( :book => b ) assert_valid e assert e.save assert_equal e.book(true), b end test "create book edition without associated book" do e = BookEdition.new assert !e.valid? end end ---------------------------------------------------------------------- test/units/book_editions_image_test.rb ---------------------------------------------------------------------- require ''test_helper'' class BookEditionsImageTest < ActiveSupport::TestCase test "relates to book editions and amazon images" do edition = BookEdition.make image = AmazonImage.make edition_image = BookEditionsImage.new( :amazon_image => image, :book_edition => edition ) assert_valid edition_image assert edition_image.save end end ---------------------------------------------------------------------- assert_valid is defined as: ---------------------------------------------------------------------- def assert_valid(record) assert record.valid?, record.errors.full_messages.join("\n") end ---------------------------------------------------------------------- Here are the associated models: app/models/book_edition.rb ---------------------------------------------------------------------- class BookEdition < ActiveRecord::Base belongs_to :book has_many :book_editions_users has_many :users, :through => :book_editions_users has_many :book_editions_images has_many :images, :through => :book_editions_images, :source => :amazon_image def validate errors.add_on_empty %w( book ) end def find_or_create_by_isbn(isbn) end end ---------------------------------------------------------------------- app/models/book.rb ---------------------------------------------------------------------- class Book < ActiveRecord::Base validates_presence_of :title has_many :editions, :class_name => ''BookEdition'' end ---------------------------------------------------------------------- app/models/book_editions_user.rb ---------------------------------------------------------------------- class BookEditionsUser < ActiveRecord::Base belongs_to :user belongs_to :book_edition end ---------------------------------------------------------------------- app/models/user.rb ---------------------------------------------------------------------- class User < ActiveRecord::Base acts_as_authentic acts_as_authorized_user acts_as_authorizable attr_accessible :username, :email, :password, :password_confirmation has_many :book_editions_users has_many :book_editions, :through => :book_editions_users, :include => :book has_and_belongs_to_many :friends, :class_name => ''User'', :association_foreign_key => ''friend_id'' has_and_belongs_to_many :friend_of, :class_name => ''User'', :foreign_key => ''friend_id'', :association_foreign_key => ''user_id'' def validate errors.add_on_empty %w( username email ) end end ----------------------------------------------------------------------
Jacob Helwig
2009-Oct-22 07:00 UTC
Re: undefined local variable or method `autosave_associated_records_for_book'' when saving record
On Sat, Oct 17, 2009 at 14:49, Jacob Helwig <jacob.helwig-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So, I''m at a loss trying to figure out where I screwed something up here. > > I''ve got (what I think should be) a fairly straight-forward record > setup, but when I try to create/save a BookEdition record, I get the > following error: NameError: undefined local variable or method > `autosave_associated_records_for_book_editions_users'' > > I''m actually getting two slightly different errors, but with almost > the exact same output: > ---------------------------------------------------------------------- > 1) Error: > test_create_valid_book_edition(BookEditionTest): > NameError: undefined local variable or method > `autosave_associated_records_for_book_editions_users'' for > #<ActiveSupport::Callbacks::Callback:0xb6e86b40> > /test/unit/book_edition_test.rb:11:in `test_create_valid_book_edition'' > > > 2) Error: > test_relates_to_book_editions_and_amazon_images(BookEditionsImageTest): > NameError: undefined local variable or method > `validate_associated_records_for_book'' for > #<ActiveSupport::Callbacks::Callback:0xb6e88b70> > vendor/gems/notahat-machinist-1.0.3/lib/machinist/active_record.rb:54:in `make'' > /test/unit/book_editions_image_test.rb:5:in > `test_relates_to_book_editions_and_amazon_images'' > ---------------------------------------------------------------------- > > The full project (with the failures) is up on GitHub ( > http://github.com/jhelwig/kamitsukai/tree/wip ), in case I miss > something that would help in tracking this down. > > Here are the two unit tests where I''m seeing this kind of error crop up: > > test/units/book_edition_test.rb > ---------------------------------------------------------------------- > require ''test_helper'' > > class BookEditionTest < ActiveSupport::TestCase > test "create valid book edition" do > b = Book.make > e = BookEdition.new( > :book => b > ) > > assert_valid e > assert e.save > assert_equal e.book(true), b > end > > test "create book edition without associated book" do > e = BookEdition.new > assert !e.valid? > end > end > ---------------------------------------------------------------------- > > test/units/book_editions_image_test.rb > ---------------------------------------------------------------------- > require ''test_helper'' > > class BookEditionsImageTest < ActiveSupport::TestCase > test "relates to book editions and amazon images" do > edition = BookEdition.make > image = AmazonImage.make > > edition_image = BookEditionsImage.new( > :amazon_image => image, > :book_edition => edition > ) > > assert_valid edition_image > assert edition_image.save > end > end > ---------------------------------------------------------------------- > > assert_valid is defined as: > ---------------------------------------------------------------------- > def assert_valid(record) > assert record.valid?, record.errors.full_messages.join("\n") > end > ---------------------------------------------------------------------- > > > Here are the associated models: > > app/models/book_edition.rb > ---------------------------------------------------------------------- > class BookEdition < ActiveRecord::Base > belongs_to :book > has_many :book_editions_users > has_many :users, > :through => :book_editions_users > has_many :book_editions_images > has_many :images, > :through => :book_editions_images, > :source => :amazon_image > > def validate > errors.add_on_empty %w( book ) > end > > def find_or_create_by_isbn(isbn) > end > end > ---------------------------------------------------------------------- > > app/models/book.rb > ---------------------------------------------------------------------- > class Book < ActiveRecord::Base > validates_presence_of :title > > has_many :editions, :class_name => ''BookEdition'' > end > ---------------------------------------------------------------------- > > app/models/book_editions_user.rb > ---------------------------------------------------------------------- > class BookEditionsUser < ActiveRecord::Base > belongs_to :user > belongs_to :book_edition > end > ---------------------------------------------------------------------- > > app/models/user.rb > ---------------------------------------------------------------------- > class User < ActiveRecord::Base > acts_as_authentic > acts_as_authorized_user > acts_as_authorizable > attr_accessible :username, :email, :password, :password_confirmation > > has_many :book_editions_users > has_many :book_editions, > :through => :book_editions_users, > :include => :book > has_and_belongs_to_many :friends, > :class_name => ''User'', > :association_foreign_key => ''friend_id'' > has_and_belongs_to_many :friend_of, > :class_name => ''User'', > :foreign_key => ''friend_id'', > :association_foreign_key => ''user_id'' > > def validate > errors.add_on_empty %w( username email ) > end > end > ---------------------------------------------------------------------- >So, to answer my own question, and in the hope of saving others the same frustration I went through: The problem was that as part of the book_editions table, I had a field named ''binding''. This caused ActiveRecord to blow up in very cryptic ways. Renaming ''binding'' to ''binding_type'' got rid of the errors.