# Relationship insert problem

**URL:** <https://www.ruby-forum.com/t/relationship-insert-problem/68524>\
**Category:** Rails\
**Created:** [September 13, 2006, 11:21am UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524 "2006-09-13T11:21:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 11:21am UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/1 "2006-09-13T11:21:25Z")

</div>

Hey all

im trying to do somthing like this…

```
if request.post?
  @customer = CustomerAnonymous.new(params[:customer])
  @customer.customer_addresses.new(params[:delivery])
end

```

However, the validation on my CustomerAnonymous model is being invoked  
no problem, but my CustomerAddress model validation isnt, any ideas or  
work arounds for this?

Is this even the best way to be inserting data into multiple tables from  
a single form?

Cheers

Tim

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 12:02pm UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/2 "2006-09-13T12:02:23Z")

</div>

Tim P. wrote:

> Hey all
> 
> im trying to do somthing like this…
> 
> ```
> if request.post?
> @customer = CustomerAnonymous.new(params[:customer])
> @customer.customer_addresses.new(params[:delivery])
> end
> 
> ```
> 
> However, the validation on my CustomerAnonymous model is being invoked  
> no problem, but my CustomerAddress model validation isnt, any ideas or  
> work arounds for this?
> 
> Is this even the best way to be inserting data into multiple tables from  
> a single form?
> 
> Cheers
> 
> Tim

Tim,

I’m assuming that you’ve got a model called CustomerAddress and that a  
CustomerAnonymous has\_one CustomerAddress, in which case I think you  
need something like this (but I’m learning too…)

if request.post?  
@customer = CustomerAnonymous.new(params[:customer])  
@customer.customer\_addresses =  
CustomerAddress.new(params[:delivery])  
# Assuming that @customer.save is coming here or soon after…  
end

I’m not sure if this is still the case, but the first edition of the  
rails book says that the CustomerAddress save will fail silently if  
anything goes wrong, so it recommends…

if request.post?  
address = CustomerAddress.new(params[:delivery])  
unless address.save! # \<-- Throws an exception if save fails.  
@customer = CustomerAnonymous.new(params[:customer])  
@customer.customer\_addresses = address  
# Assuming that @customer.save is coming here or soon after…  
end

Hope that helps.

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/3 "2006-09-13T12:14:41Z")

</div>

Excellent, i have that first edition of the rails book but i couldnt  
find i page on that for love nor money!!

Ill give that a whirl now!

Cheers

Tim

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 12:38pm UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/4 "2006-09-13T12:38:12Z")

</div>

Tim P. wrote:

> Excellent, i have that first edition of the rails book but i couldnt  
> find i page on that for love nor money!!

It’s in the section that describes one-to-one associations in the  
active-record basics chapter, I don’t have the book with me to give you  
the page number unfortunately.

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 1:26pm UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/5 "2006-09-13T13:26:02Z")

</div>

Ok cool, i am getting somwhere now, however i think there is a problem  
witht he fact that one customer could have many addresses… im getting  
the error…

undefined method `each’ for #CustomerAddress:0x226cde0

out of

activerecord-1.14.2/lib/active\_record/base.rb:1792:in `method_missing' activerecord-1.14.2/lib/active_record/associations/association_collection.rb:118:in`replace’  
activerecord-1.14.2/lib/active\_record/associations.rb:895:in  
`customer_addresses=' #{RAILS_ROOT}/app/controllers/store/checkout_controller.rb:34:in`anonymous\_checkout’

Any suggestions? as one customer could have many different address on my  
system.

My code currently looks like

```
if request.post?
  address = CustomerAddress.new(params[:delivery])
  if address.save! # <-- Throws an exception if save fails.
    @customer = Customer.new(params[:customer])
    @customer.customer_addresses = address
    begin
      @customer.save!
    rescue => @error
      flash[:notice] = 'Could not be saved.'
    end
  else
    flash[:notice] = 'address not valid in model'
  end
end

```

Cheers

Tim

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/6 "2006-09-13T13:39:32Z")

</div>

Interstingly…

```
if request.post?
  address = CustomerAddress.new(params[:delivery])
  unless address.save! # <-- Record sucseeds validation, and 

```

insertes a record in DB  
@customer = Customer.new(params[:customer])  
@customer.customer\_addresses = address # \<-- this is the  
problem line where the ‘each’ exception is comming from.  
begin  
@customer.save!  
rescue =\> @error  
flash[:notice] = ‘Could not be saved.’  
end  
else  
flash[:notice] = ‘address not valid in model’  
end  
end

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 2:33pm UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/7 "2006-09-13T14:33:12Z")

</div>

Tim P. \<rails-mailing-list@…\> writes:

> problem line where the ‘each’ exception is comming from.  
> begin  
> customer.save!  
> rescue =\> error  
> flash[:notice] = ‘Could not be saved.’  
> end  
> else  
> flash[:notice] = ‘address not valid in model’  
> end  
> end

for a has\_one:  
customer.customer\_address = address

for a has\_many:  
customer.customer\_addresses \<\< address

where \<\< is usually used to append something (address) to something else  
(customer.customer\_addresses)

Gareth

---

<div class="post-metadata">

**Author:** ![timperrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c5a1d2/32.png) [@timperrett](https://www.ruby-forum.com/u/timperrett)\
**Post date:** [September 13, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/relationship-insert-problem/68524/8 "2006-09-13T14:36:41Z")

</div>

Excellent, i had manged to work out that it was using an array type  
thing, as customer.customer\_addresses[0] also inserted it in the correct  
place, but the \<\< solution is a far slicker example!

Many thanks for all you replys, this is what i ended up doing, well at  
least its a start, i can add more error checking and presentaion from  
here! Many thanks again!

Tim

```
if request.post?
  @customer = CustomerAnonymous.new(params[:customer])
  @customer.customer_addresses << 

```

CustomerAddress.new(params[:delivery])  
begin  
if @customer.save!  
begin  
@order = Order.update(session[:order\_id], :customer\_id =\>  
@customer.id)  
rescue =\> @error  
flash[:notice] = ‘Your order could not be updated.’  
end  
end  
rescue =\> @error  
flash[:notice] = ‘Please ensure you have filled in all the  
required feilds (’ + @error + ‘)’  
end  
end
