rubyonrailsin

A Ruby and Rails talk

Sunday, March 28, 2010


[Rails] Re: Adding data to database directly through Ruby code

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

On Mar 28, 7:23 pm, Derek Cannon <li...@ruby-forum.com> wrote:
>
> 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.

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment

Subscribe feeds via e-mail

Blog Archive