View Full Version : [newbie] adding new rows with rails???
jonny_d
March 16th, 2006, 03:08 PM
Hello all,
i've been working with RoR for a few months now and it's finally starting to make more sense to me. but now i've run into a problem. is it possible to add rows to tables through rails? i know that it comes with security risks but i'm sure there's ways around that. right? any help is much appreciated.
jon
rob
March 16th, 2006, 06:38 PM
Let's say you have a table called customers, and the model is Customer.
You can add a customer programatically like this:
Customer.create( :name => "Robert Oliver", :address => "Rubyland" )
However, you'll probably want to do something like this:
@customer = Customer.new @params[:customer]
A great way to learn the ins and outs of creation of rows is to look at the generated scaffolds. They contain all the necessary code in the controllers for adding rows, etc.
jonny_d
March 17th, 2006, 11:16 AM
hey thanks but i think i should have worded that differentley. i'm cool with all the scaffold stuff. i actually have a pretty cool app going, but i was wondering something different.
say i have a table called employees. and that table contained name, email, and phone. what if i wanted to add another row, say street_address, to that table. can i do that with rails? sorry for that but thanks for the help.
rob
March 17th, 2006, 07:02 PM
Oh, that's a column, not a row.
You don't have to do anything to your data model, but you do have to add the column to MySQL. Use MySQL Query Browser, phpMyAdmin, or execute SQL similar to this:
ALTER TABLE employees ADD COLUMN street_address VARCHAR(255);
motobass
March 17th, 2006, 10:02 PM
Hey, and if you're keeping all this stuff in source control, you could run try running a 'migration' on it. If you google 'rails rake migration' you should get something useful.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.