Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic Rails question: manually inserting a row into a database table

I'm learning Rails and it's going well so far. My biggest question at the moment is: how do I go about manually inserting a row into my database? I've got the scaffolding in place for creating rows of DataTypeOne, but I want a row for DataTypeTwo to be created when the form for DataTypeOne is submitted (and have it reference the id of DataTypeOne...but I think I can work this out on my own).

Thanks in advance.

like image 374
Adam Rezich Avatar asked Jun 26 '26 10:06

Adam Rezich


1 Answers

You create rows in your database by creating and saving new ActiveRecord Objects (your models).

So in your controller code you could create a new row of DataTypeTwo by doing

new_record = DataTypeTwo.new
new_record.save!
like image 164
TonyLa Avatar answered Jun 27 '26 23:06

TonyLa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!