# Multiple models one form and a one-to-one relationship

**URL:** <https://www.ruby-forum.com/t/multiple-models-one-form-and-a-one-to-one-relationship/161266>\
**Category:** Rails\
**Created:** [February 24, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/multiple-models-one-form-and-a-one-to-one-relationship/161266 "2009-02-24T22:29:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 24, 2009, 10:29pm UTC](https://www.ruby-forum.com/t/multiple-models-one-form-and-a-one-to-one-relationship/161266/1 "2009-02-24T22:29:11Z")

</div>

Hi!

I’ve got a not so unfamiliar multiple models in one form problem. Due  
to the complex forms railscast I’ve got it up and running for a one-to-  
many relationship but now I have a one-to-one relationship and I  
didn’t get it done.

Here is my code, perhaps someone can take a look and give me a hint  
what’s wrong. I put what is going wrong into the code where I think it  
is belonging to. And I don’t know if it matters but I’m running Rails  
2.1.1.

## 

# Model

class Subscription \< ActiveRecord::Base  
belongs\_to :user

def user=(attributes)  
##  
# !!! Here is something wrong  
# - I get a Hash with all my fields here  
# - attributes belongs to class HashWithIndifferentAccess  
# - Whith self I get the error „undefined method  
`stringify\_keys!’…“  
# - Without self the subscription model is saved without the user  
model

```
self.build_user(attributes)

```

end  
end

class Subscription \< ActiveRecord::Base  
has\_one :subscription  
end

## 

# Controller

class SubscriptionsController \< ApplicationController  
def new  
@subscription = Subscription.new  
@subscription.build\_user

```
respond_to do |wants|
  wants.html
  wants.xml { render :xml => @subscription }
end

```

end

def create  
@subscription = Subscription.new(params[:subscription])

```
respond_to do |wants|
  if @subscription.save
    flash[:notice] = 'Subscription was successfully created.'
    wants.html { redirect_to root_url }
    wants.xml { render :xml => @subscription, :status

```

=\> :created, :location =\> @payment }  
else  
wants.html { render :action =\> “new” }  
wants.xml { render :xml =\> @subscription.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end  
end

## 

# View (Haml syntax)

- form\_for :subscription, @subscription, :url =\> subscriptions\_path do  
|subscription\_form|

Every help is appreciated. Thanks in advance.

Regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 24, 2009, 11:06pm UTC](https://www.ruby-forum.com/t/multiple-models-one-form-and-a-one-to-one-relationship/161266/2 "2009-02-24T23:06:22Z")

</div>

In a has\_one/belongs\_to association, I don’t think you need the user=  
method. What happens if you take it out completely?

---

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 24, 2009, 11:08pm UTC](https://www.ruby-forum.com/t/multiple-models-one-form-and-a-one-to-one-relationship/161266/3 "2009-02-24T23:08:44Z")

</div>

Try self.build\_user(:name=\>attributes) , name or whatever the column  
is.

---

<div class="post-metadata">

**Author:** ![Stefan\_Frede](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5e9695/32.png) [@Stefan\_Frede](https://www.ruby-forum.com/u/Stefan_Frede)\
**Post date:** [February 24, 2009, 11:31pm UTC](https://www.ruby-forum.com/t/multiple-models-one-form-and-a-one-to-one-relationship/161266/4 "2009-02-24T23:31:05Z")

</div>

Without the user method in the subscription model I get the following  
error:

User(#17369790) expected, got HashWithIndifferentAccess(#2700040)

I’ve changed the user method:

def user=(attributes)  
attributes.each do |attribute|  
self.build\_user(attribute.first =\> attribute.last)  
end  
end

I get no error and the subscripton model is saved, but not the user  
model.

These are my parameters:

…“subscription”=\>{  
“user”=\>{  
“name”=\>“vorname”,  
“city”=\>“ort”,  
“postal\_code”=\>“plz”,  
“street”=\>“straße”,  
“phone”=\>“telefon”,  
“c\_o”=\>“c/o (optional)”,  
“surname”=\>“nachname”,  
“comment”=\>“kommentar (optional)”,  
“email”=\>“e-mail”  
}  
}…

For me they are not looking that bad.
