(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. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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! On Mar 28, 11:23 pm, Derek Cannon <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> (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. > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Frederick Cheung
2010-Mar-28 21:42 UTC
Re: Adding data to database directly through Ruby code
On Mar 28, 7:23 pm, Derek Cannon <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.