# Inserting data to another DB table

**URL:** <https://www.ruby-forum.com/t/inserting-data-to-another-db-table/63027>\
**Category:** Rails\
**Created:** [June 10, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/inserting-data-to-another-db-table/63027 "2006-06-10T21:07:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 10, 2006, 9:07pm UTC](https://www.ruby-forum.com/t/inserting-data-to-another-db-table/63027/1 "2006-06-10T21:07:46Z")

</div>

I have a form under one controller/model called “billing” using the  
“bills” table. When this form completes I want it to change the  
“has\_billing” field for the user under the “users” table from “0” to  
“1”. I’ve almost got it. I put:

user = User.new  
user.update\_attribute(“has\_billing”, “1”)

under the “create” function for the billing form. It creates a new user  
in the “users” table and does apply “1” to the “has\_billing” field.

How can I get it to change the field under the user submitting the form?  
I have the session data saved under the “@session” variable and can  
access it using “@session[‘user’].id”

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 10, 2006, 9:40pm UTC](https://www.ruby-forum.com/t/inserting-data-to-another-db-table/63027/2 "2006-06-10T21:40:53Z")

</div>

how about

@session[‘user’].has\_billing = 1  
@session[‘user’].save

if that doesnt work , you can go the longer way round

@user = @session[‘user’]  
@user.has\_billing = 1  
@user.save

or if that doesnt work you can go even longer  
@user = User.find(@session[‘user’].id)  
@user.has\_billing = 1  
@user.save

but i think the first should work…

d.

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 10, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/inserting-data-to-another-db-table/63027/3 "2006-06-10T21:55:41Z")

</div>

David Andersen wrote:

> how about
> 
> @session[‘user’].has\_billing = 1  
> @session[‘user’].save
> 
> if that doesnt work , you can go the longer way round
> 
> @user = @session[‘user’]  
> @user.has\_billing = 1  
> @user.save
> 
> or if that doesnt work you can go even longer  
> @user = User.find(@session[‘user’].id)  
> @user.has\_billing = 1  
> @user.save
> 
> but i think the first should work…

A couple coding style tips:

Say don’t use the `@session` inst var, instead use the `session`  
accessor. It’s better to use the API than dig into inst vars even if you  
can.

Don’t store the user object in the session. Instead you should store the  
user\_id in the session and grab the user object from the db when you  
need it. You’ll avoid any number of synchronization and serialization  
issues. And in general symbols are better than strings for hash keys,  
since they compare by object id instead of by string contents.

if user = User.find\_by\_id(session[:user\_id])  
user.has\_billing = 1  
user.save  
end

find\_by\_id is a nice way to find a model object because it returns nil  
if there isn’t one for that id.

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![jasbur](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@jasbur](https://www.ruby-forum.com/u/jasbur)\
**Post date:** [June 10, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/inserting-data-to-another-db-table/63027/4 "2006-06-10T22:41:55Z")

</div>

None of David’s solutions worked for me. But I did end up using a  
combination of my and David’s code. The line I had wrong was:

user = User.new

Once I used David’s suggestion to assign the id from the @session  
variable to it’s own variable:

@user = User.find(@session[‘user’].id)

Then I used my call to change the attribute using the new variable:

@user.update\_attribute(“has\_billing”, “1”)

So, the whole thing goes:

def create  
@bill = Bill.new(params[:bill])  
@bill.user\_id = @session[‘user’].id  
@user = User.find(@session[‘user’].id)  
@user.update\_attribute(“has\_billing”, “1”)  
…more code…

Thanks guys
