I have some legacy DB with many tables (around 100) the old system is run using php and have so many operation with data used in NLP.
I was a bit curious how thing could be done with Rails. I read that ActiveRecord is no good for legacy systems! but I sow some people used it with no problems, So why some clime that?
Also considering the following php code, with many If & SQL statements, some clime that such case is considered as typical data/database task not OO one, so it’s better to stay away from AcriveRecord or any other ORM. Is this true? if not, can it be done easily by Rails’s ActiveRecord?
Assuming all variables are submitted from a web form. we have some text to be annotate and we have to see if there’re any other users that agree with this user who submitted the form. Then we’ll update the agree table.
if ($submitted_id !=""){
$sql = "SELECT * FROM annotation WHERE relation_id = $relation_id";
$result = open_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$user_id = $row["user_id"];
$line = "";
$sql = "SELECT * FROM agree WHERE user_id = $user_id AND user_id_agree = $submitted_id LIMIT 1";
$result = open_query($sql);
$line = mysql_fetch_array($result, MYSQL_ASSOC);
if ( $user_id != $submitted_id ) {
if ( $line == "" ) {
mysql_query ("INSERT INTO agree VALUES ('$user_id' , '$submitted_id', '1', CURRENT_TIMESTAMP)");
} else {
$total = $line["total"];
$new_total = $total+1;
mysql_query ("UPDATE agree SET total = $new_total WHERE user_id = $user_id AND user_id_agree = $submitted_id LIMIT 1");
}
}
}
}
Regards
Well, using legacy database schemas with ActiveRecord makes you lose some of its advantages. For instance, Rails takes a “convention over configuration” approach to many things, and ActiveRecord inherits this. For instance, if you have a model called Product, Rails assumes your table will be called “products”. For this reason, you don’t have to configure the table name. The same holds through for foreign keys, URL conventions, etc. The more you are willing to make your application work the way Rails works, the easier a time you will have. If your goal going in is not “build an application that does X”, but rather “convert this application to Ruby and ActiveRecord”, the resulting code will not be as clear or idiomatic.
If you try to write an application in the way that is conventional for the tool you are using, you will likely get clearer, more maintainable code as a result.
As for the code you provided, it’s not entirely clear to me what it is supposed to do (since I do not have the context to know what “submitted_it”, “user_id_agree”, “annotation”, and so on mean in your application.