> 1. If i input valid data in the required field, but type the
> passwordconfirmation so that it will fail I get the error message:
> ArgumentError which I guess has something to do with the
"raise"-line
> below.
See below for the fix.> def create_data
> @customer = Customer.new
> @identity = Identity.new
> @person = Person.new
>
> @identity.customer = @customer
> @person.identity = @identity
These last two lines won''t do what you want them to do. You cannot
set a relationship like this until the record with the foreign key
has been saved. Eg.
@customer.save
@identity.costomer = @customer
If you jump the gun like you did then @customer.id will be nil so
@identity.customer_id will be nil. Same with @identity.id and
@person.identity_id. But doing what I suggest is a paradox since you
don''t want to save it yet. I struggle with this same paradox all the
time. No one has ever given me a good solutions, only hacks (see hack
below).> @customer.save
> @identity.save
> @person.save
>
> if(undigestedPassword != params[:passwordconfirm])
> @pwdmismatch = true
> end
>
> raise ActiveRecord::RecordInvalid unless
> @customer.valid? && @identity.valid? && @person.valid?
&&
> (@pwdmismatch
> == false)
@customer.save calls @customer.valid? and returns the result. You are
repeating validation several times here.
The grand solution/hack is something like:
==================================def create_data
@customer = Customer.new
@identity = Identity.new
@person = Person.new
#too soon for this to work
#@identity.customer = @customer
#@person.identity = @identity
if request.post?
@pwdmismatch = false
@customer.attributes = params[:customer]
@identity.attributes = params[:identity]
@person.attributes = params[:person]
undigestedPassword = @customer.password
@customer.password = MD5.md5(undigestedPassword).hexdigest
begin
@customer.transaction(@customer, @identity, @person) do
customer_valid = @customer.save
@identity.customer = @customer
identity_valid = @identity.save
@person.identity = @identity
person_valid = @person.save
if(undigestedPassword != params[:passwordconfirm])
@pwdmismatch = true
end
raise ActiveRecord::RecordInvalid.new(@customer)
unless
customer_valid && identity_valid && person_valid &&
(@pwdmismatch
== false)
login(@customer.username, undigestedPassword,
"identity") #Login and redirect the user to the selected identity
end #end transaction
rescue ActiveRecord::RecordInvalid
end
end
end #end of create data
============
-John
--
John Smilanick
Computing Staff - Webmaster
Kavli Institute for Theoretical Physics
University of California, Santa Barbara
jsmilani@kitp.ucsb.edu
(805) 893-6307
On Mar 29, 2006, at 1:38 PM, Daniel wrote:
> I have three tables customers, indentities and people. I hav ecreated
> models for them. The are connected with foreign keys and
> has_many/belongs_to in this fashion:
>
> customers has_many: identities
> identities belongs_to: customer
> identities has_many: people
> people belongs_to: identities
>
> Now, I have a form in which I would like to create db entries for the
> three tables. customers have a password field that needs to be
> equal to
> the params[:passwordconfirm], otherwise none of the models should be
> saved to the db.
>
> I also have some data checks (validated_presence_of and
> validates_uniqueness_of) in the models.
>
> If the data checks or password confirmation fails, I want that the
> form
> i displayed again so that the user can correct the errors. Else the
> data
> shoud be saved to the db. If I input data in all required fields
> all is
> fine, but...
>
> My problems are:
>
> 1. If i input valid data in the required field, but type the
> passwordconfirmation so that it will fail I get the error message:
> ArgumentError which I guess has something to do with the
"raise"-line
> below.
>
> 2. If I omitt some of the required fields I get the following error
> message:
> ActiveRecord::StatementInvalid ...Column ''customer_id''
cannot be
> null:
> INSERT INTO identities...
>
> The controller action looks like this. I guess the code inside the
> transaction block contains errors... however I would need some
> help in
> finding out whats wrong. Please help me!
>
> def create_data
> @customer = Customer.new
> @identity = Identity.new
> @person = Person.new
>
> @identity.customer = @customer
> @person.identity = @identity
>
> if request.post?
> @pwdmismatch = false
> @customer.attributes = params[:customer]
> @identity.attributes = params[:identity]
> @person.attributes = params[:person]
> undigestedPassword = @customer.password
> @customer.password = MD5.md5(undigestedPassword).hexdigest
> begin
> @customer.transaction(@customer, @identity,
> @person) do
> @customer.save
> @identity.save
> @person.save
>
> if(undigestedPassword != params[:passwordconfirm])
> @pwdmismatch = true
> end
>
> raise ActiveRecord::RecordInvalid unless
> @customer.valid? && @identity.valid? && @person.valid?
&&
> (@pwdmismatch
> == false)
> login(@customer.username, undigestedPassword,
> "identity") #Login and redirect the user to the selected identity
> end #end transaction
> rescue ActiveRecord::RecordInvalid
> end
> end
> end #end of create data
>
> My rhtml-file uses "error_message_on",
"error_messages_for" and
> checking
> if pwdmismatch is true to mark errors in the input data.
>
> Like I said, I would need some help on this.
>
>
> Best regards,
> Daniel
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060329/5b215f73/attachment-0001.html