0

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...

1
  • er, maybe I'm overthinking this. user.address.nil? Commented Aug 7, 2011 at 12:40

1 Answer 1

2

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?.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.