views:

688

answers:

7

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.

+4  A: 

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!
TonyLa
How do you specify the column data?
Unniloct
Nevermind, I got it, thanks!
Unniloct
+1  A: 

This is such a common thing to want to do that you should be able to do it through your models and not have to think about doing anything manually to the database.

For example, if you want DataTypeTwo to reference DataTypeOne, is that because DataTypeTwo 'belongs to' DataTypeOne? Or does DataTypeOne 'have many' things of DataTypeTwo? Rails has conventions to express all these kinds of relationships and others. Saving the object and related items to the DB should then be automatic when you call the 'save!' method on the relevant object.

You should take a look at a few more rails examples and tutorials to grok this. Unless you are doing something very unusual you shouldn't need to do more than annotate your models and follow the conventions, so that rails knows what belongs to what.

frankodwyer
Basically I have Nodes and Connectors. Connectors connect Nodes to more Nodes. When I create a new Node, I want the user to be able to create new Connectors that don't lead to new Nodes yet...like "red links" in Wikipedia.
Unniloct
Should these nodes / connections act as a graph or tree of some kind? you will find annotations / plugins for this kind of thing in rails too. Look for plugins like acts_as_graph / acts_as_tree etc.
frankodwyer
+1  A: 

You can also use the following shortcut:

DataTypeTwo.create

The create method will create a new DataTypeTwo object and save the record to the database.

Yaser Sulaiman
+4  A: 

As frankodwyer said, you really want to be using the Rails associations to state the fact that one Model is related to another. If there is a one to one relationship, then you would use

belongs_to

and

has_one

And depending on which model has the foreign key, you use one or the other. In your example, it sounds like DataTypeTwo has the foreign key, back to DataTypeOne. So you would have something like this:

class DataTypeTwo < ActiveRecord::Base
  belongs_to :data_type_one
end


class DataTypeOne < ActiveRecord::Base
  has_one :data_type_two
end

And you can create records with the foreign keys like this

one = DataTypeOne.create(...)
two = DataTypeTwo.create(...)
two.data_type_one = one
two.save

There are some shortcuts, but this is the verbose gist of it. Assuming all your model names and foreign keys can be obtained by reflection, then you're OK, otherwise you have to explicitly name the keys.

All of this is covered in detail in the ActiveRecord docs:

http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Cody Caughlan
+1  A: 

In addition to setting up appropriate ActiveRecord associations as Cody Caughlan describes, it sounds like you ought to make use of ActiveRecord callbacks to guarantee that when you create a DataTypeOne, a DataTypeTwo is created and associated with the DataTypeOne. Depending on your needs, I would guess that the before_create or after_create would do the trick.

For lots more details, see the Rails documentation for ActiveRecord::Callbacks.

Ed Ruder
+1  A: 

If you need to be more flexible, active records offers also a sql interface for most SQL operations: Database Statements

amaruk
+1  A: 

You dont need to use scaffolding just to create entries in the database. All you need to do is

./script/console (from your project folder)

And that will give you a command line driven full Rails environment for your project. You can try out anything here. In your context, you could do

record = DataTypeTwo.new(:field1 => value1, :field2 => value2)
record.save

The console is also the best way to try out stuff before putting it into your code

krishashok