views:

147

answers:

1

If I have two models:

class Post < ActiveRecord::Base
  belongs_to :user
end

and

class User < ActiveRecord::Base
  has_many :posts
end

If I do:

post = Post.new
user = User.new
post.user = user
post.save

Does the user get saved as well and the primary key properly assigned in post's user_id field?

+2  A: 

ActiveRecord belong_to assosications has the ability to be autosaved along with the parent model, but the functionality is off by default. To enable it:

class Post < ActiveRecord::Base
  belongs_to :user, :autosave => true
end
Josh
Weird. I turned that flag on, and doing the same as above still gives me `> post.errors #=> #<OrderedHash {:user_id=>["can't be blank"]}>` and `user.new_record? #=> true`. Am I missing something?
obvio171