On Sep 9, 3:33 am, fireflyman <yangxiwenh...@gmail.com> wrote: > So,Could you help me deal with my code ?
David already gave you the answer, you are trying to add a has_many :through associated record to an unsaved record. The error message is pretty much telling you that "Cannot associate new records through 'Book#authors_books' on '#'. Both records must have an id in order to create the has_many :through record associating them.". The association is made through the author_books table with the id of both, the author and the book. With the book id being nil... I think you get the point. If you use Book.new (and thats what you are doing in your test), the new books id is still nil because it hasn't been saved to the database yet. Replace Book.create and you should be fine (as long as your validations don't fail). -- pascal --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---