Salil Gaikwad
2009-Jul-07 13:02 UTC
Adding data in two tables simul;taneously with Validations
Hi All, I have 2 tables 1] user_infos & 2] users class UserInfo has_one :user validates_presence_of :city class User belongs_to :user_info validates_presence_of :first_name i write following code in my create method. @user_info = UserInfo.new(params[:user_info]) @user=@user_info.build_user(:first_name=>'''') if @user_info.save else end now what i want is to create a object that save data in two table at a time and if my first name and city is blank it should throw an error that user info is not saved because of two errors 1]City can''t be blank. 2]Firts name can''t be blank. Regards, Salil Gaikwad -- Posted via http://www.ruby-forum.com/.
Vitaly Kushner
2009-Jul-07 15:17 UTC
Re: Adding data in two tables simul;taneously with Validations
There is an easier and better way to do the same. Just use Rails nested model forms: http://ryandaigle.com/articles/2008/7/19/what-s-new-in-edge-rails-nested-models Hope this helps, -- Vitaly Kushner http://twitter.com/vkushner Founder, Astrails Ltd. http://astrails.com Check out our blog: http://blog.astrails.com On Jul 7, 4:02 pm, Salil Gaikwad <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi All, > > I have 2 tables > 1] user_infos & > 2] users > > class UserInfo > has_one :user > validates_presence_of :city > > class User > belongs_to :user_info > validates_presence_of :first_name > > i write following code in my create method. > > @user_info = UserInfo.new(params[:user_info]) > @user=@user_info.build_user(:first_name=>'''') > > if @user_info.save > > else > > end > > now what i want is to create a object that save data in two table at a > time and if my first name and city is blank it should throw an error > that > user info is not saved because of two errors > 1]City can''t be blank. > 2]Firts name can''t be blank. > > Regards, > > Salil Gaikwad > -- > Posted viahttp://www.ruby-forum.com/.