Hi all, I am a rails newbie and have some problems with my first app. I ran the login generator script because I needed basic authentication. Everything seems to be ok. Now I want to have settings for each user and thought of having a foreign key called "user_id" in the settings table which refers to the "id" in the users table as described in the "Agile ...rails" book. So I wrote something like : add constraint fk_settings_user foreign key (user_id) references users (id) and added "belongs_to :user" in my model(setting.rb) Now I expect the "user_id" to be automatically filled with the value of "id" from the users table, when a user signs up an fills in some user settings. But something is wrong here: the value of "user_id" remains NULL :-( Any ideas? I have the following tables (sorry some german stuff in it): CREATE TABLE `settings` ( `id` int(11) unsigned NOT NULL auto_increment, `allergy` varchar(120) default NULL, `instrumente` varchar(120) default NULL, `vergetarier` tinyint(1) unsigned NOT NULL default ''0'', `kocherfahrung` tinyint(1) unsigned NOT NULL default ''3'', `user_id` int(11) unsigned default NULL, PRIMARY KEY (`id`), KEY `fk_settings_user` (`user_id`) ) CREATE TABLE `users` ( `id` int(11) unsigned NOT NULL auto_increment, `login` varchar(80) default NULL, `password` varchar(40) default NULL, PRIMARY KEY (`id`), KEY `login` (`login`) ) Thanks for your answers (and of course for the fish;-) and sorry for the long Email! Regards Ana Kovatcheva
Ana, When you create your setting record, you''ll need to manually associate the setting record with the appropriate user record. For example, lets say after I create a new user, I also setup their default settings: class User < ActiveRecord::Base ... def after_create setting = Setting.new ...default settings go here... setting.user = self # associate the newly created user with the setting record setting.save # save the newly created setting to the database end ... Hope that helps, Derek On 6/26/05, Ana Kovatcheva <ani-m5qINXy6OmT9gL3VM1WMYg@public.gmane.org> wrote:> Hi all, > > I am a rails newbie and have some problems with my first app. > > I ran the login generator script because I needed basic > authentication. Everything seems to be ok. > Now I want to have settings for each user and thought of having a > foreign key called "user_id" in the settings table which refers to > the "id" in the users table as described in the "Agile ...rails" book. > > So I wrote something like : > > add constraint fk_settings_user foreign key (user_id) references users > (id) > > and added "belongs_to :user" in my model(setting.rb) > > > Now I expect the "user_id" to be automatically filled with the value > of "id" from the users table, when a user signs up an fills in some > user settings. > > But something is wrong here: the value of "user_id" remains NULL :-( > Any ideas? > > > I have the following tables (sorry some german stuff in it): > > CREATE TABLE `settings` ( > `id` int(11) unsigned NOT NULL auto_increment, > `allergy` varchar(120) default NULL, > `instrumente` varchar(120) default NULL, > `vergetarier` tinyint(1) unsigned NOT NULL default ''0'', > `kocherfahrung` tinyint(1) unsigned NOT NULL default ''3'', > `user_id` int(11) unsigned default NULL, > PRIMARY KEY (`id`), > KEY `fk_settings_user` (`user_id`) > ) > > > CREATE TABLE `users` ( > `id` int(11) unsigned NOT NULL auto_increment, > `login` varchar(80) default NULL, > `password` varchar(40) default NULL, > PRIMARY KEY (`id`), > KEY `login` (`login`) > ) > > Thanks for your answers (and of course for the fish;-) and sorry for > the long Email! > > > Regards > Ana Kovatcheva > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Derek Haynes HighGroove Studios - http://www.highgroove.com Keeping it Simple. 404.593.4879
Hi Derek, thank you so much - I got it ... regards Ana Am 26.06.2005 um 18:38 schrieb Derek Haynes:> Ana, > > When you create your setting record, you''ll need to manually associate > the setting record with the appropriate user record. > > For example, lets say after I create a new user, I also setup their > default settings: > > class User < ActiveRecord::Base > ... > def after_create > setting = Setting.new > ...default settings go here... > setting.user = self # associate the newly created user with the > setting record > setting.save # save the newly created setting to the database > end > ... > > Hope that helps, > > Derek > > > > > On 6/26/05, Ana Kovatcheva <ani-m5qINXy6OmT9gL3VM1WMYg@public.gmane.org> wrote: > >> Hi all, >> >> I am a rails newbie and have some problems with my first app. >> >> I ran the login generator script because I needed basic >> authentication. Everything seems to be ok. >> Now I want to have settings for each user and thought of having a >> foreign key called "user_id" in the settings table which refers to >> the "id" in the users table as described in the "Agile ...rails" >> book. >> >> So I wrote something like : >> >> add constraint fk_settings_user foreign key (user_id) references >> users >> (id) >> >> and added "belongs_to :user" in my model(setting.rb) >> >> >> Now I expect the "user_id" to be automatically filled with the value >> of "id" from the users table, when a user signs up an fills in some >> user settings. >> >> But something is wrong here: the value of "user_id" remains >> NULL :-( >> Any ideas? >> >> >> I have the following tables (sorry some german stuff in it): >> >> CREATE TABLE `settings` ( >> `id` int(11) unsigned NOT NULL auto_increment, >> `allergy` varchar(120) default NULL, >> `instrumente` varchar(120) default NULL, >> `vergetarier` tinyint(1) unsigned NOT NULL default ''0'', >> `kocherfahrung` tinyint(1) unsigned NOT NULL default ''3'', >> `user_id` int(11) unsigned default NULL, >> PRIMARY KEY (`id`), >> KEY `fk_settings_user` (`user_id`) >> ) >> >> >> CREATE TABLE `users` ( >> `id` int(11) unsigned NOT NULL auto_increment, >> `login` varchar(80) default NULL, >> `password` varchar(40) default NULL, >> PRIMARY KEY (`id`), >> KEY `login` (`login`) >> ) >> >> Thanks for your answers (and of course for the fish;-) and sorry for >> the long Email! >> >> >> Regards >> Ana Kovatcheva >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > > -- > Derek Haynes > HighGroove Studios - http://www.highgroove.com > Keeping it Simple. > 404.593.4879 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >