# Belongs\_to + create\_\* = rails vs. console behavior difference = confused Pepe

**URL:** <https://www.ruby-forum.com/t/belongs-to-create-rails-vs-console-behavior-difference-confused-pepe/175439>\
**Category:** Rails\
**Created:** [September 14, 2009, 12:43am UTC](https://www.ruby-forum.com/t/belongs-to-create-rails-vs-console-behavior-difference-confused-pepe/175439 "2009-09-14T00:43:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [September 14, 2009, 12:43am UTC](https://www.ruby-forum.com/t/belongs-to-create-rails-vs-console-behavior-difference-confused-pepe/175439/1 "2009-09-14T00:43:19Z")

</div>

Hi,

I have been battling something for a good hour and a half and finally  
realized how to ‘solve’ the issue but I am very confused as of why I  
should do what I just did.

I have 2 classes:

class User  
has\_many :audits  
…  
end

class Audit  
belongs\_to :user  
…  
end

The way things need to work is to first create an audit and after the  
audit is created a user that has access to the audit gets created.  
This has to work this way and not the other way around.

I have been testing my code in the console with the following:

audit = Audit.create(…)  
audit.create\_user(…)

In all instances ‘audit’ ended up being updated correctly with the  
user ID.

However, when I executed a very similar code in my controller I got  
different results.

My original control code:  
@audit = Audit.new(params[:audit])  
…  
if @audit.save  
@audit.create\_user(…)  
unless @audit.tenant\_id  
# this code is never hit  
end  
end

Everything seems OK since the @audit.tenant\_id seemed to have a value  
but when the code was done running the database showed that the user  
was created but the audit never got updated with the user ID. In order  
to make it work I had to modify the code above as:

@audit = Audit.new(params[:audit])  
…  
if @audit.save  
@audit.create\_user(…)  
@audit.save # \<------- Why do I need to do this?  
unless @audit.tenant\_id  
# this code is never hit  
end  
end

I am very confused because the console updates the audit record but  
the controller doesn’t.

??? Why ???

I’d appreciate an explanation of the difference in behavior.

Thank you.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [September 14, 2009, 3:21am UTC](https://www.ruby-forum.com/t/belongs-to-create-rails-vs-console-behavior-difference-confused-pepe/175439/2 "2009-09-14T03:21:21Z")

</div>

On Sun, Sep 13, 2009 at 6:41 PM, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> In all instances ‘audit’ ended up being updated correctly with the  
> unless @audit.tenant\_id  
> Why tenant\_id and not user\_id? I’m assuming that this is a typo or  
> you’ve changed your code to ‘protect the innocent’ and missed a  
> change.

I’ll assume you meant user\_id when you typed tenant\_id.

> ```
> # this code is never hit
> 
> ```
> 
> end  
> end
> 
> Everything seems OK since the @audit.tenant\_id seemed to have a value  
> but when the code was done running the database showed that the user  
> was created but the audit never got updated with the user ID. In order  
> to make it work I had to modify the code above as:

Okay, let’s think about what those associations mean.

class User  
has\_many :audits  
# means that we can find the audits for the user by finding all of  
the rows in the audit table which have the key for this  
# User  
# There’s no column for this association in the user table itself

class Audit  
belongs\_to :user  
# This means that the audit table has a column, conventionally  
called user\_id which holds the key of the user which  
# ‘has’ this audit, or equivalently to which this audit belongs.

> @audit = Audit.new(params[:audit])

This creates a new Audit, which has the attribute values provided  
by params[:audit]. Presumably this doesn’t include a user\_id value,  
so the new instance of audit will have a nil value for user\_id, so

> …  
> if @audit.save

This saves the newly created audit in the db, and since user\_id is  
nil then the column value for user\_id will be null

> @audit.create\_user(…)

This creates a new user and saves it which assigns an id for the user,  
it sets the user\_id value in the instance of Audit, but DOES NOT SAVE  
it.

> @audit.save # \<------- Why do I need to do this?  
> unless @audit.tenant\_id  
> # this code is never hit  
> end  
> end

It’s rare that you create a model with a has\_many attribute from the  
model which belongs\_to it, it’s normally done the other way around.  
For one thing since a User has many audits, you don’t normally create  
a user each time you create an audit.

You would normally create and audit FOR a user rather than the other  
way around either by:

```
user = User.find(user_id) #where user id came from an action

```

parameter, or a form value

then either

```
audit.create(:user => user, ....)

```

or

```
user.audits.create(audit_parameters)

```

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
