user483040
user483040

Reputation:

nested forms in rails 3

I'm trying to setup a nested form and the main thing I can't get working is knowing when to do the build method. Here are my models:

class User < ActiveRecord::Base
  belongs_to :address
  accepts_nested_attributes_for :address
end

class Address < ActiveRecord::Base
  has_one :user
end

In all the examples I've found, which appear to be pre-rails 3, I see the following two methods:

user.address.empty?
user.address.build

I have managed to winnow out with my semi-asleep Google-Fu that the build has been replaced with:

user.build_address

What I'm trying to figure out is what's the replacement for empty? It doesn't appear to exist when I try things at the console, but user.build_address is wonderful.

Any ideas?

thanks...

Upvotes: 0

Views: 139

Answers (1)

Hck
Hck

Reputation: 9167

For the has_one relation build_address will create an empty address instance or will load already existing instance. So you don't need to call user.address.empty?.

Upvotes: 2

Related Questions