SQL Files and Ruby on Rails - mysql

So I'm kinda new with Ruby on Rails. In one of my Database classes the teacher asked us to create a simple application using any tool I liked. The app just has to have the ability to get new users, let users make appointments and add patients (it's for doctors), all that just enter information to the database and quering the database for information.
The problem is that in the specifications of this project says that every interaction with the database has to be done in "Raw SQL", so I can't use the Ruby On Rails utilities. I have been working with this framework for about six months, so I'm familiarized with it, and I know how to make the "Raw SQL" for the seeding of the database, and the queries, but I don't know how to start the project from my database.sql file, where I create the schema and all the tables.
This can be with any DBMS, I currently have a script for sql server and mysql.
How can I create my project using the sql file to create the database instead of using rails migrations?
Thanks in advance!

By "Raw SQL" does your teacher mean all the SQL, including the CREATE TABLE statements? Or just the SELECT statements? You can do both, raw, in Rails, but can you use standard Rails migrations?
Rails is like learning assembler by learning C. You learn assembler better when not lost in an endless sea of it. Rails wraps up relations so you don't need to spend all day writing redundant JOIN clauses.
Here's the answer to the "raw SQL?" FAQ in Rails: http://apidock.com/rails/ActiveRecord/Querying/find_by_sql . I would wrap that in a cute logger method that writes each SELECT statement to a formatted file, so you can turn that in as your homework. But you can tell I didn't do very well in school...

Related

Trying to build rails 3.2 app over existing mysql db

I am trying to figure out how to build a rails app on top of an existing mysql db. I think the best method would just be to create a migration with the same layout as the existing db, but I am not quite sure how to do this, then connect it. I am aware of this post Building Ruby on Rails App with an existing Mysql db
but am still unsure; do I just do this but with the columns I need? Also the main answer to this question is saying that I should make my db a csv and then import it, does anyone have a tutorial or gem they recommend for that?
I have not done this exact task personally although when I modify my databases manually through my mysql client and create backup tables for example, they magically appear in my schema.rb file later down the road when I run some future migrations.
So the following post should help or at least point you in the right direction:
http://tianhsky.ueuo.com/blog/2012/02/20/generate-schema-rb-from-existing-database/
Before that, try to learn more about rails and it's conventions. Probably you'll need to adapt your database scheme.
Or you could start an application and then import the data, even by SQL or by CSV as you mentioned. Migrating data can be a tedious work, but a necessary one.
You can check this gem to see if it helps on your case, because it will depend on your actual schema.

generate dynamic insert queries from mysql tables using php

i have a courses_material Mysql database. It has many tables like
courses, lessons, exercises, quizzes, exams, questions
i'm running my application in 2 servers, one is the TEST SERVER and other is the PRODUCTION SERVER
basically we will create material of a course XYZ in test server and check it in the application for look and feel, course content etc..
If everything is fine then we will move the entire material of course XYZ to production database.
Test server may contain any other courses.
I want to select a course and dump all the data of that course into .sql file in the form of insert queries. Then i can run the sql file on the production site.
I have to do this using PHP, i'm using codeigniter (MVC) framework.
What is the best way to do it?.
i need some suggestions
CodeIgniter has a db utility class with a backup function. You could try using that or take the source and manipulate it to fit your needs.

Doing SQL queries in Rails with Variables - Converting a PHP/Codeigniter Application

I'm converting a PHP app written in CodeIgniter to Rails.
Due to the size of the application and the inability to makes change to the schema, I'm not interested in making models for each entity.
I want to be able to do CodeIgniter style queries like this (Supply a pre-written query and associated variables):
query("SELECT * FROM posts WHERE id = ?", id)
and get a result back.
I know that it's highly suggested to use models and do it "the rails way", and I definitely plan to for my next application that I write from scratch in Rails, but for this project I want to avoid the ORM and just have the ability to do direct queries using the database connection.
What would be the best way to do this?
I hate myself for not having the willpower to not answer this question, so just quickly:
Use ActiveRecord#find_by_sql

Ruby on Rails3 and MySQL query best practices

I'm trying to transition from PHP to Rails and was wondering what the best practices are. I have all the data I need in MySQL and would like to make simple queries and display them using the kinds of niceties afforded by ActiveRecords. I'm familiar with the MVC paradigm and am trying not to rely on my knowledge of PHP, but how, for example, would I be able to do the kinds of things I used to do with:
$query = mysql_query("Select * from table where name = 'blah');
if I already have that data (I can see generating a scaffold if I didn't have pre-existing data). Do I create a scaffold and then import my data into it? or do I write raw MySQL queries in Ruby?
Thanks!
First, remember that Rails is a web dev framework, not a scripting language like PHP. Ruby is the scripting language. PHP has frameworks as well, but just wanted to make sure you realize the difference between the one and the other.
Most basic and medium-complexity queries in Rails are handled via the ActiveRecord methods and helpers. You'll find that you'll be writing much less actual SQL, as that is generally abstracted away into the framework.
For instance, assuming the name of your model is City, the equivalent of your query in Rails would be:
City.find_all_by_name('blah')
How does this work? When you create a model in Rails you usually subclass ActiveRecord::Base. Built into that class is a plethora of functionality that, for one thing, examines your data table and builds dynamic finders for each of the fields and combination of fields on the table.
Since you already have all the data, you're going to be overriding some of the conventional functionality of Rails in order to get everything working. For instance, Rails assumes by convention that there is a primary key field named "id". Also, Rails assumes that the table is named as the plural form of whatever the model class definition is. All of these things are defaulted by convention, but can be overridden. If you were building from scratch and following conventions, all this would sort of happen as a matter of course.
Based on your question, I think you need to spend some time with some basic Rails reading material and some specific info about ActiveRecord and Rails models, so that you can come up to speed on these major differences between Rails and standard PHP application. If you don't get that straight from the beginning, then you are going to build a lot of PHP-style Rails stuff and you won't be taking full advantage of what Rails and Ruby have to offer. In the end, you'll say "what did I do all that for, it's all the same."
If, instead, you try to start from the Rails Way of doing things, you'll find that Ruby and Rails offer a lot.
If you are working with an existing database then you are going to have a hard time forcing Rails to play nicely with it. Rails power (some might call it a weakness) is that it favors convention over configuration. In other words, it really expects you database columns to be structured a certain way. Stray from the convention and you lose all the reasons for using Rails in the first place.
When you start a new rails project and use the migrations rails makes sure that the structure is as it expects.
If you are moving an old project to rails then I would strongly suggest writing a function to import that data into a rails-created DB. This will save you a lot of heartache in the long run and will allow you to take full advantage of Rails' strengths.

Massive Wordpress database migration

This is a doozy of a question...
I am developing a new wordpress website to replace my client's massively sized blog (tens of thousands of pages). Their existing blog is built on a custom PERL blogging platform from 1992...so as you can imagine, it needs updating.
My job is to find the BEST possible way to migrate all of their existing data into a format that wordpress can understand.
The new theme is very advanced, and this job is very advanced as well. After searching for specialists, I can't seem to find anyone who specialized in this field. What would you do in this situation? Thanks!
I've done this before, it's not that hard. I approached it as a wordpress plugin. First get the plugin to connect to the old database and get it to pull the information you need. Then you can use native wordpress function to insert new users, posts, comments, etc.
I did it line by line, which isn't the most efficient approach, but it is the easiest. I used an AJAX front-end to display the conversion status and repeatedly call the converter back-end, as the actual migration took up to several hours.
If the site can't be shut down for that long while you migrate the database, you could either look at doing a proper bulk export/import, or lock old topics and migrate those over first.
My converter was for an old PHP nuke site, and due to the fact that we were using WP-United, I didn't have to worry too much about user credentials and comments. However, the code might help you get started: http://www.wp-united.com/releases/php-nuke-converter
I have done a couple of these Wordpress migrations. The theme you're using in Wordpress is really not that big a deal, most likely. Themes in Wordpress don't ordinarily impact the database structure.
The Wordpress side is easy, it's in MySQL in most cases. The place to start for you would be to determine how the data in the PERL blog is stored. If it's a custom blog solution, there probably isn't a script you can find to do the migration. Hopefully, it's in a data form that will allow you to do a data dump in a format MySQL will allow you to import using something like phpMyAdmin (a popular GUI for MySQL). At that point, you can create a MySQL statement to match up the relevant fields in your old data with those in MySQL. If you're not comfortable doing it and want an expert, the thing to do is find out how the PERL blog stores it's data and find someone who is familiar with both that format and MySQL.