Hi,everyone
Here is my model code--->
------------------------------------------------------------------------
has_many :authors_books
has_many :authors, :through => :authors_books
belongs_to :book
belongs_to :author
has_many :authors_books
has_many :books, :through => :authors_books
---------------------------------------------------------------------------
I use the authors_books to associate book and author.
And then ,I write a unit test,book_test.rb---->the code like follow
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def test_ferret
Book.rebuild_index
assert Book.find_with_ferret("Pride and Prejudice")
assert_difference Book, :count do
book = Book.new(:title => 'The Success of Open Source',
:published_at => Time.now, :page_count =>
500,
:price => 59.99, :isbn => '0-647-01292-5')
book.authors << Author.create(:first_name => "Steven", :last_name =>
"Weber")
book.publisher = Publisher.find(1)
assert book.valid?
book.save
assert_equal 1, Book.find_with_ferret("Open Source").size
assert_equal 1,Book.find_with_ferret("Steven Weber").size
end
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When I run my test,and goes wrong .Like this--->
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1) Error:
test_ferret(BookTest):
ActiveRecord::HasManyThroughCantAssociateNewRecords: Cannot associate
new record
s through 'Book#authors_books' on '#'. Both records must have an id in
order to
create the has_many :through record associating them.
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/associat
ions/has_many_through_association.rb:52:in `<<'
test/unit/book_test.rb:87:in `test_ferret'
./test/unit/../test_helper.rb:41:in `assert_difference'
test/unit/book_test.rb:82:in `test_ferret'
C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/
active_support/testin
g/default.rb:7:in `run'
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Yeah ,I know my test code was wrong ? But I don't know how to alter my
code !
Could you give me a favor ? Thanks a lot !
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---