I have a ruby (on rails) class:
class User < ActiveRecord::Base
# relationships
belongs_to :current_shipping_address, :class_name => "Address"
belongs_to :subscription
# Validators
validates_presence_of :subscription
validates_presence_of :current_shipping_address
end
I do this in a controller:
subscription = Subscription.new
address_info = params[:user].delete(:address) rescue {}
@address = Address.new(address_info.merge(:country => "US"))
@user = User.new(params[:user].merge(:first_name => @address.first_name, :last_name => @address.last_name))
@user.subscription = subscription
@user.current_shipping_address = @address
@user.save!
At this point, incredibly, I have a @user that has been saved in the database but has no current_shipping_address (despite the validation). The subscription has also been saved in the database.
The address does NOT get saved.
What am I missing here? 1 - how does user get saved without the validation failing? 2 - why is the address not saved?
How can I alter this code so that the address gets saved (as I would expect it to)?
I am running this under ruby on rails 3.
Thanks!