before the update_attributes line, try adding b = Book.find(:first, "title => 'rails'")
then b.update_attributes! :author_id => a2.id It seems that update_attributes will work from found object, not newly created object, and this is normally done under update action of controller Sean K wrote: > Under Rails 2.3.2 using a completely brand new project, I have 2 > models: > > class Author < ActiveRecord::Base > # name:string > end > > class Book < ActiveRecord::Base > # title:string > belongs_to :author # author_id > end > > And a simple test where i create a Book with an Author using the > belongs_to and then update the foreign key directly: > > require 'test_helper' > > class BookTest < ActiveSupport::TestCase > test "foreign key updating" do > a1 = Author.create! :name => 'author 1' > b = Book.create! :title => 'rails', :author => a1 > a2 = Author.create! :name => 'author 2' > b.update_attributes! :author_id => a2.id > assert_equal a2.id, b.author_id # author_id is still a1.id! > end > end > > The test fails on the last line. Am i doing something wrong or is > this expected behaviour? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

