# Adding data to database directly through Ruby code

**URL:** <https://www.ruby-forum.com/t/adding-data-to-database-directly-through-ruby-code/185895>\
**Category:** Rails\
**Created:** [March 28, 2010, 8:23pm UTC](https://www.ruby-forum.com/t/adding-data-to-database-directly-through-ruby-code/185895 "2010-03-28T20:23:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [March 28, 2010, 8:23pm UTC](https://www.ruby-forum.com/t/adding-data-to-database-directly-through-ruby-code/185895/1 "2010-03-28T20:23:53Z")

</div>

(First, I’m very new to Rails and still slightly new to Ruby. So bare  
with me.)

I made a Ruby application that allows me to view local XML files which  
contain information on my favorite movies. My program reads the XML  
files and loads them into variables (such as @title = "The Terminator,  
@year = “1984”, etc). All of this is done in a class called Movie, so I  
get all the XML files from a directory, then create an array of Movie  
classes which all hold the information for their specific XML file.

(For the visual thinkers, that’s: my\_dir.each { |i|  
my\_array.push(Movie.new(i)) })

Everything works fine, and I can organize the data however I please.

But now that I’m getting started with Rails, I’d like to create a  
controller that – instead of displaying the movie information to the  
console – would write these variables into a SQLite3 database. I’ve  
looked around, and I’ve already got the database set up, and with  
Scaffolding, I can add/edit/remove entries manually, but I’m still  
unsure of how to:

1. Copy/paste my Ruby code into my Rails project. (Does the class I’ve  
created simply go into a new controller, or is there some other  
convention for where to put complete Ruby classes?)
2. How to add the variables directly to the database, so I don’t have to  
manually enter the data.

If anyone could help me with this, I’d greatly appreciate it. If you’re  
curious as to my level of knowledge in Rails, the only things I have a  
fairly good grasp on are: how to start a server, how to view the pages  
on the server, how to create a database (with all the tables I need),  
how to create a scaffolding, and what the difference is between  
controllers and the actual view page.

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [March 28, 2010, 9:16pm UTC](https://www.ruby-forum.com/t/adding-data-to-database-directly-through-ruby-code/185895/2 "2010-03-28T21:16:45Z")

</div>

1. You have to create controllers with the class names and then you  
can copy the code in the controllers.
2. You can create a model and a database with all the fields you want  
and then you can insert the data in the db from the variables using a  
controller.

I am also a beginner at Rails. So, please confirm this with someone  
else as well.  
Happy coding!

---

<div class="post-metadata">

**Author:** ![derek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b38774/32.png) [@derek](https://www.ruby-forum.com/u/derek)\
**Post date:** [March 28, 2010, 11:43pm UTC](https://www.ruby-forum.com/t/adding-data-to-database-directly-through-ruby-code/185895/3 "2010-03-28T23:43:39Z")

</div>

On Mar 28, 7:23 pm, Derek C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> convention for where to put complete Ruby classes?)  
> 2. How to add the variables directly to the database, so I don’t have to  
> manually enter the data.

What you would typically do is create a subclass of ActiveRecord::Base  
called Movie. Whenever you create and save and object of that class a  
row appears in the movies table. Instead of creating instances of your  
current Movie class, create instances of your new ActiveRecord derived  
one. Your xml parsing code can probably stay pretty much the same,  
except that you’ll either want to do Movie.create(hash\_of\_attributes)  
or do Movie.new and then set the attributes one by one.

Fred
