Validates_associated bug?

i seem to be experiencing a bug where rails tells me that my
organization object’s associated credit card is invalid (activerecord
validation-wise invalid), when i know it to be valid. this happens
only if i tell the organization to validate_associated :credit_card.

i know the associated credit card is valid, because a) that
credit_card object itself throws no validation errors, and b) if i
change @credit_card.save to
@credit_card.update_attributes(params[:credit_card]) i get a new
credit_card record in the database, yet the overall transaction fails
later because @organization still thinks the credit card is invalid.

taking the validates_associated :credit_card out of Organization lets
the whole operation complete normally, using the same input data.

i have a model structure roughly like so:

User
id, etc…

BusinessUser
id, etc…
user_id

Organization
id, etc…
primary_business_user_id
credit_card_id

CreditCard
id, etc…

and a business_user_controller to create it all:

def create

@user = User.new(params[:user])
@business_user = BusinessUser.new(params[:business_user])
@business_user.user = @user
@organization = Organization.new(params[:organization])
@organization.primary_user = @business_user
@credit_card = CreditCard.new
credit_card_validated = true

has_credit_card = params[:has_credit_card] == ‘true’
if has_credit_card
@credit_card = CreditCard.new(params[:credit_card])
credit_card_validated = @credit_card.save
@organization.credit_card = @credit_card
end

begin
User.transaction do
if @user.save! and
#((has_credit_card and @credit_card.save!) or true) and
(credit_card_validated and true or raise
ActiveRecord::RecordNotSaved) and
@organization.save! and
@business_user.save!
flash[:notice] = ‘Account created. Check your email for login
details.’
Mailer::deliver_signup_confirmation(@user)
redirect_to :controller => ‘login’, :action => ‘login’
end

end # end User.transaction

rescue
render :action => ‘new’
end
end

can anyone offer some insight with what’s here, or do you want me to
zip and post the entire working example? i’ve been pulling my hair
out for a week now poring over debug output from this :-/

thanks,
jeff

no ideas anyone? do you need more info?

On 8/18/06, jeff emminger [email protected] wrote:

i know the associated credit card is valid, because a) that

credit_card_id
@business_user.user = @user
end
created. Check your email for login details.’
end

can anyone offer some insight with what’s here, or do you want me to
zip and post the entire working example? i’ve been pulling my hair
out for a week now poring over debug output from this :-/

It all comes down to that validates_associated? Axe it, move on!
You’ll
‘aha’ and solve it later.

jeremy