Forum: Ruby on Rails inserting into database problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
57014aac041f53d610b8609247d07385?d=identicon&s=25 Anthony Ward (ant284)
on 2009-05-15 16:21
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-15 17:08
(Received via mailing list)
2009/5/15 Anthony Ward <rails-mailing-list@andreas-s.net>

> :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.
57014aac041f53d610b8609247d07385?d=identicon&s=25 Anthony Ward (ant284)
on 2009-05-15 17:24
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 Law wrote:
> 2009/5/15 Anthony Ward <rails-mailing-list@andreas-s.net>
>
>> :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.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-05-15 17:35
(Received via mailing list)
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/)
as
are the railscasts (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 Ward <rails-mailing-list@andreas-s.net>
This topic is locked and can not be replied to.