简体   繁体   English

Ruby on Rails3 和 MySQL 查询最佳实践

[英]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.我正在尝试从 PHP 过渡到 Rails,并且想知道最佳实践是什么。 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.我在 MySQL 中拥有我需要的所有数据,并希望使用 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:我熟悉 MVC 范式,并且试图不依赖我对 PHP 的了解,但是,例如,我将如何能够做我曾经做过的事情:

$query = mysql_query("Select * from table where name = 'blah'); $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?还是我在 Ruby 中编写原始 MySQL 查询?

Thanks!谢谢!

First, remember that Rails is a web dev framework, not a scripting language like PHP.首先,记住 Rails 是一个 web 开发框架,而不是像 PHP 这样的脚本语言。 Ruby is the scripting language. Ruby 是脚本语言。 PHP has frameworks as well, but just wanted to make sure you realize the difference between the one and the other. PHP 也有框架,但只是想确保您意识到两者之间的区别。

Most basic and medium-complexity queries in Rails are handled via the ActiveRecord methods and helpers. Rails 中的大多数基本和中等复杂度查询都是通过 ActiveRecord 方法和帮助程序处理的。 You'll find that you'll be writing much less actual SQL, as that is generally abstracted away into the framework.你会发现你写的实际 SQL 会少得多,因为它通常被抽象到框架中。

For instance, assuming the name of your model is City, the equivalent of your query in Rails would be:例如,假设您的 model 的名称是 City,那么您在 Rails 中的查询相当于:

City.find_all_by_name('blah')

How does this work?这是如何运作的? When you create a model in Rails you usually subclass ActiveRecord::Base.当你在 Rails 中创建 model 时,你通常继承 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.内置在 class 中的是大量的功能,一方面,检查您的数据表并为表上的每个字段和字段组合构建动态查找器。

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.由于您已经拥有所有数据,因此您将覆盖 Rails 的一些常规功能以使一切正常运行。 For instance, Rails assumes by convention that there is a primary key field named "id".例如,Rails 按照惯例假定有一个名为“id”的主键字段。 Also, Rails assumes that the table is named as the plural form of whatever the model class definition is.此外,Rails 假设该表被命名为 model class 定义的复数形式。 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.根据您的问题,我认为您需要花一些时间阅读一些基本的 Rails 阅读材料和一些关于 ActiveRecord 和 Rails 模型的具体信息,以便您了解 Rails 和标准 PHP 应用程序之间的这些主要差异。 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.如果您从一开始就没有明白这一点,那么您将构建大量 PHP 风格的 Rails 东西,您将无法充分利用 Rails 和 Ruby 所提供的功能。 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.相反,如果您尝试从 Rails 的处事方式开始,您会发现 Ruby 和 Rails 提供了很多。

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 很好地使用它。 Rails power (some might call it a weakness) is that it favors convention over configuration. Rails 的力量(有些人可能称之为弱点)在于它更倾向于约定而不是配置。 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.偏离惯例,你一开始就失去了使用 Rails 的所有理由。

When you start a new rails project and use the migrations rails makes sure that the structure is as it expects.当您开始一个新的 Rails 项目并使用迁移 rails 时,请确保结构符合预期。

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.如果您要将旧项目移动到 Rails,那么我强烈建议您编写 function 以将该数据导入到 rails 创建的数据库中。 This will save you a lot of heartache in the long run and will allow you to take full advantage of Rails' strengths.从长远来看,这将为您省去很多心痛,并让您充分利用 Rails 的优势。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM