# Inserting into database problem

**URL:** <https://www.ruby-forum.com/t/inserting-into-database-problem/168081>\
**Category:** Rails\
**Created:** [May 15, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/inserting-into-database-problem/168081 "2009-05-15T16:21:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [May 15, 2009, 4:21pm UTC](https://www.ruby-forum.com/t/inserting-into-database-problem/168081/1 "2009-05-15T16:21:31Z")

</div>

Hi,

I have this problem I have been working on ours and can’t find the  
proper solution

i have two tables  
User\_Info : columns {:id, :name, :address}

```
Access_Info : columsn {:id, :username, :password, :user_info_fkey,

```

:access\_type}

In my model  
User\_Info  
when I had I do something like this

@user= User\_Info.new(params[:user])  
@access = Access\_Info.new(params[:access])

then i do a transaction to see if one save fails delete everything that  
was created.

my problem is with Access\_Info.new I want to add manually more  
parameters values. first I need to get the id from @user and give it to  
access\_info and I also want to manually set the value of :access\_type

i did something like this

@user= User\_Info.new(param[:user])  
params[:access].merge= ( {:user\_info\_fkey =\> params[:user][:id] } )  
@access = Access\_Info.new(param[:access])

but it does not seem to work… which is annoying…

also just another quick questions  
when using save is there a way to say to insert on columns x,y,z ??  
because I have a column that uses NOW()+0 and I don’t want ruby to write  
null or something else

thank you

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [May 15, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/inserting-into-database-problem/168081/2 "2009-05-15T17:08:22Z")

</div>

2009/5/15 Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> :access\_type}
> 
> my problem is with Access\_Info.new I want to add manually more  
> parameters values. first I need to get the id from @user and give it to  
> access\_info and I also want to manually set the value of :access\_type

I know it is not answering the question, but if you are trying to get  
the  
user id into the access record then do it by using an association.  
UserInfo has\_one :access\_info  
AccessInfo belongs\_to :user\_info

Let Rails do the hard work for you. Also I would advise sticking to the  
Rails conventions if you can, don’t have underscores in the model names,  
they will just cause confusion. In fact I would suggest the models could  
be  
User and Access.

To set the value of access\_type just do @access.access\_type = whatever  
(Again should it just be @access.type ?)

Colin

> also just another quick questions  
> when using save is there a way to say to insert on columns x,y,z ??  
> because I have a column that uses NOW()+0 and I don’t want ruby to write  
> null or something else

Don’t understand this question.

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [May 15, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/inserting-into-database-problem/168081/3 "2009-05-15T17:24:10Z")

</div>

Hi,

I tried to set to the convention but I caused loads of problems…  
anyaways  
I have set up  
UsersInfo has\_one :access\_info  
AccessInfo belongs\_to :user\_info

but it won’t do any work, how would ruby knows that my column  
“access” refers to refers to UserInfo.

should i do  
UserInfo has\_one :access\_info, :foreign\_key =\> ‘access\_type’

then would ruby put  
in access\_type userinfo.id automatically?

Colin L. wrote:

> 2009/5/15 Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > :access\_type}
> > 
> > my problem is with Access\_Info.new I want to add manually more  
> > parameters values. first I need to get the id from @user and give it to  
> > access\_info and I also want to manually set the value of :access\_type
> 
> I know it is not answering the question, but if you are trying to get  
> the  
> user id into the access record then do it by using an association.  
> UserInfo has\_one :access\_info  
> AccessInfo belongs\_to :user\_info
> 
> Let Rails do the hard work for you. Also I would advise sticking to the  
> Rails conventions if you can, don’t have underscores in the model names,  
> they will just cause confusion. In fact I would suggest the models could  
> be  
> User and Access.
> 
> To set the value of access\_type just do @access.access\_type = whatever  
> (Again should it just be @access.type ?)
> 
> Colin
> 
> > also just another quick questions  
> > when using save is there a way to say to insert on columns x,y,z ??  
> > because I have a column that uses NOW()+0 and I don’t want ruby to write  
> > null or something else
> 
> Don’t understand this question.

---

<div class="post-metadata">

**Author:** ![shinkaku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/4da419/32.png) [@shinkaku](https://www.ruby-forum.com/u/shinkaku)\
**Post date:** [May 15, 2009, 5:35pm UTC](https://www.ruby-forum.com/t/inserting-into-database-problem/168081/4 "2009-05-15T17:35:26Z")

</div>

Can I suggest you do some reading up on the basics of Rails to get you  
going? The rails guides are very good ([http://guides.rubyonrails.org/](http://guides.rubyonrails.org/))  
as  
are the railscasts ([http://railscasts.com/](http://railscasts.com/)), though I would suggest  
starting  
with the guides. There are also a number of books, I found Agile Web  
Development With Rails 3rd edition (earlier editions will not be correct  
for  
latest Rails) very good.

In addition stick around here and for any posts that you understand the  
question, follow the replies and try to understand them. You will learn  
a  
tremendous amount over a period. In particular you will keep find  
yourself  
saying “Wow I didn’t know you could do that”.

2009/5/15 Anthony W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
